File: current_scope.feature

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 (87 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download
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
Feature: `RSpec` provides the current scope as `RSpec.current_scope`

  You can detect which rspec scope your helper methods or library code is executing in.
  This is useful if for example, your method only makes sense to call in a certain context.

  Scenario: Detecting the current scope
    Given a file named "current_scope_spec.rb" with:
      """ruby
      # Outside of the test lifecycle, the current scope is `:suite`
      exit(1) unless RSpec.current_scope == :suite

      at_exit do
        exit(1) unless RSpec.current_scope == :suite
      end

      RSpec.configure do |c|
        c.before :suite do
          expect(RSpec.current_scope).to eq(:before_suite_hook)
        end

        c.before :context do
          expect(RSpec.current_scope).to eq(:before_context_hook)
        end

        c.before :example do
          expect(RSpec.current_scope).to eq(:before_example_hook)
        end

        c.around :example do |ex|
          expect(RSpec.current_scope).to eq(:before_example_hook)
          ex.run
          expect(RSpec.current_scope).to eq(:after_example_hook)
        end

        c.after :example do
          expect(RSpec.current_scope).to eq(:after_example_hook)
        end

        c.after :context do
          expect(RSpec.current_scope).to eq(:after_context_hook)
        end

        c.after :suite do
          expect(RSpec.current_scope).to eq(:after_suite_hook)
        end
      end

      RSpec.describe "RSpec.current_scope" do
        before :context do
          expect(RSpec.current_scope).to eq(:before_context_hook)
        end

        before :example do
          expect(RSpec.current_scope).to eq(:before_example_hook)
        end

        around :example do |ex|
          expect(RSpec.current_scope).to eq(:before_example_hook)
          ex.run
          expect(RSpec.current_scope).to eq(:after_example_hook)
        end

        after :example do
          expect(RSpec.current_scope).to eq(:after_example_hook)
        end

        after :context do
          expect(RSpec.current_scope).to eq(:after_context_hook)
        end

        it "is :example in an example" do
          expect(RSpec.current_scope).to eq(:example)
        end

        it "works for multiple examples" do
          expect(RSpec.current_scope).to eq(:example)
        end

        describe "in nested describe blocks" do
          it "still works" do
            expect(RSpec.current_scope).to eq(:example)
          end
        end
      end
      """
    When I run `rspec current_scope_spec.rb`
    Then the examples should all pass