File: shared_context_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (130 lines) | stat: -rw-r--r-- 3,239 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
RSpec.describe RSpec::SharedContext do
  it "is accessible as RSpec::Core::SharedContext" do
    RSpec::Core::SharedContext
  end

  it "is accessible as RSpec::SharedContext" do
    RSpec::SharedContext
  end

  it "supports before and after hooks" do
    before_all_hook = false
    before_each_hook = false
    after_each_hook = false
    after_all_hook = false
    shared = Module.new do
      extend RSpec::SharedContext
      before(:all) { before_all_hook = true }
      before(:each) { before_each_hook = true }
      after(:each)  { after_each_hook = true }
      after(:all)  { after_all_hook = true }
    end
    group = RSpec.describe do
      include shared
      example { }
    end

    group.run

    expect(before_all_hook).to be(true)
    expect(before_each_hook).to be(true)
    expect(after_each_hook).to be(true)
    expect(after_all_hook).to be(true)
  end

  include RSpec::Core::SharedExampleGroup::TopLevelDSL

  it "runs the before each hooks in configuration before those of the shared context" do
    ordered_hooks = []
    RSpec.configure do |c|
      c.before(:each) { ordered_hooks << "config" }
    end

    RSpec.shared_context("before each stuff", :example => :before_each_hook_order) do
      before(:each) { ordered_hooks << "shared_context"}
    end

    group = RSpec.describe "description", :example => :before_each_hook_order do
      before(:each) { ordered_hooks << "example_group" }
      example {}
    end

    group.run

    expect(ordered_hooks).to be == ["config", "shared_context", "example_group"]
  end

  it "supports let" do
    shared = Module.new do
      extend RSpec::SharedContext
      let(:foo) { 'foo' }
    end
    group = RSpec.describe do
      include shared
    end

    expect(group.new.foo).to eq('foo')
  end

  it 'supports overriding let without warnings' do
    shared = Module.new do
      extend RSpec::SharedContext
      let(:foo) { 'foo' }
    end
    group = RSpec.describe do
      include shared
      let(:foo) { 'bar' }
    end

    expect(group.new.foo).to eq('bar')
  end

  it "supports let when applied to an individual example via metadata" do
    shared = Module.new do
      extend RSpec::SharedContext
      let(:foo) { "bar" }
    end

    RSpec.configuration.include shared, :include_it

    ex = value = nil
    RSpec.describe "group" do
      ex = example("ex1", :include_it) { value = foo }
    end.run

    expect(ex.execution_result).to have_attributes(:status => :passed, :exception => nil)
    expect(value).to eq("bar")
  end

  it 'supports explicit subjects' do
    shared = Module.new do
      extend RSpec::SharedContext
      subject { 17 }
    end

    group = RSpec.describe do
      include shared
    end

    expect(group.new.subject).to eq(17)
  end

  %w[describe context].each do |method_name|
    it "supports nested example groups using #{method_name}" do
      shared = Module.new do
        extend RSpec::SharedContext
        send(method_name, "nested using describe") do
          example {}
        end
      end
      group = RSpec.describe do
        include shared
      end

      group.run

      expect(group.children.length).to eq(1)
      expect(group.children.first.examples.length).to eq(1)
    end
  end
end