summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/runner/shared_spec.rb
blob: 791588fdca68622768a11a49e1d0bd2bc2537c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'spec_helper'
require 'mspec/runner/shared'
require 'mspec/runner/context'
require 'mspec/runner/example'

describe Object, "#it_behaves_like" do
  before :each do
    ScratchPad.clear

    MSpec.setup_env

    @state = ContextState.new "Top level"
    @state.instance_variable_set :@parsed, true

    @shared = ContextState.new :shared_spec, :shared => true
    MSpec.stub(:retrieve_shared).and_return(@shared)
  end

  it "creates @method set to the name of the aliased method" do
    @shared.it("an example") { ScratchPad.record @method }
    @state.it_behaves_like :shared_spec, :some_method
    @state.process
    ScratchPad.recorded.should == :some_method
  end

  it "creates @object if the passed object" do
    object = Object.new
    @shared.it("an example") { ScratchPad.record @object }
    @state.it_behaves_like :shared_spec, :some_method, object
    @state.process
    ScratchPad.recorded.should == object
  end

  it "creates @object if the passed false" do
    object = false
    @shared.it("an example") { ScratchPad.record @object }
    @state.it_behaves_like :shared_spec, :some_method, object
    @state.process
    ScratchPad.recorded.should == object
  end

  it "sends :it_should_behave_like" do
    @state.should_receive(:it_should_behave_like)
    @state.it_behaves_like :shared_spec, :some_method
  end

  describe "with multiple shared contexts" do
    before :each do
      @obj = Object.new
      @obj2 = Object.new

      @state2 = ContextState.new "Second top level"
      @state2.instance_variable_set :@parsed, true
    end

    it "ensures the shared spec state is distinct" do
      @shared.it("an example") { ScratchPad.record [@method, @object] }

      @state.it_behaves_like :shared_spec, :some_method, @obj

      @state.process
      ScratchPad.recorded.should == [:some_method, @obj]

      @state2.it_behaves_like :shared_spec, :another_method, @obj2

      @state2.process
      ScratchPad.recorded.should == [:another_method, @obj2]
    end

    it "ensures the shared spec state is distinct for nested shared specs" do
      nested = ContextState.new "nested context"
      nested.instance_variable_set :@parsed, true
      nested.parent = @shared

      nested.it("another example") { ScratchPad.record [:shared, @method, @object] }

      @state.it_behaves_like :shared_spec, :some_method, @obj

      @state.process
      ScratchPad.recorded.should == [:shared, :some_method, @obj]

      @state2.it_behaves_like :shared_spec, :another_method, @obj2

      @state2.process
      ScratchPad.recorded.should == [:shared, :another_method, @obj2]
    end
  end
end