File: modules.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 (152 lines) | stat: -rw-r--r-- 5,363 bytes parent folder | download | duplicates (6)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
Feature: Define helper methods in a module

  You can define helper methods in a module and include it in your example
  groups using the `config.include` configuration option. `config.extend` can be
  used to extend the module onto your example groups so that the methods in the
  module are available in the example groups themselves (but not in the actual
  examples).

  You can also `include` or `extend` the module onto only certain example groups
  by passing a metadata hash as the last argument. Only groups that match the
  given metadata will `include` or `extend` the module. You can also specify
  metadata using only symbols.

  Note that examples that match a `config.include` module's metadata will also have the module included. RSpec treats every example as having a singleton example group (analogous to Ruby's singleton classes) containing just the one example.

  Background:
    Given a file named "helpers.rb" with:
      """ruby
      module Helpers
        def help
          :available
        end
      end
      """

  Scenario: Include a module in all example groups
    Given a file named "include_module_spec.rb" with:
      """ruby
      require './helpers'

      RSpec.configure do |c|
        c.include Helpers
      end

      RSpec.describe "an example group" do
        it "has access to the helper methods defined in the module" do
          expect(help).to be(:available)
        end
      end
      """
    When I run `rspec include_module_spec.rb`
    Then the examples should all pass

  Scenario: Extend a module in all example groups
    Given a file named "extend_module_spec.rb" with:
      """ruby
      require './helpers'

      RSpec.configure do |c|
        c.extend Helpers
      end

      RSpec.describe "an example group" do
        puts "Help is #{help}"

        it "does not have access to the helper methods defined in the module" do
          expect { help }.to raise_error(NameError)
        end
      end
      """
    When I run `rspec extend_module_spec.rb`
    Then the examples should all pass
    And the output should contain "Help is available"

  Scenario: Include a module in only some example groups
    Given a file named "include_module_in_some_groups_spec.rb" with:
      """ruby
      require './helpers'

      RSpec.configure do |c|
        c.include Helpers, :foo => :bar
      end

      RSpec.describe "an example group with matching metadata", :foo => :bar do
        it "has access to the helper methods defined in the module" do
          expect(help).to be(:available)
        end
      end

      RSpec.describe "an example group without matching metadata" do
        it "does not have access to the helper methods defined in the module" do
          expect { help }.to raise_error(NameError)
        end

        it "does have access when the example has matching metadata", :foo => :bar do
          expect(help).to be(:available)
        end
      end
      """
    When I run `rspec include_module_in_some_groups_spec.rb`
    Then the examples should all pass

  Scenario: Extend a module in only some example groups
    Given a file named "extend_module_in_only_some_groups_spec.rb" with:
      """ruby
      require './helpers'

      RSpec.configure do |c|
        c.extend Helpers, :foo => :bar
      end

      RSpec.describe "an example group with matching metadata", :foo => :bar do
        puts "In a matching group, help is #{help}"

        it "does not have access to the helper methods defined in the module" do
          expect { help }.to raise_error(NameError)
        end
      end

      RSpec.describe "an example group without matching metadata" do
        puts "In a non-matching group, help is #{help rescue 'not available'}"

        it "does not have access to the helper methods defined in the module" do
          expect { help }.to raise_error(NameError)
        end
      end
      """
    When I run `rspec extend_module_in_only_some_groups_spec.rb`
    Then the examples should all pass
    And the output should contain "In a matching group, help is available"
    And the output should contain "In a non-matching group, help is not available"

  Scenario: Use symbols as metadata
    Given a file named "symbols_as_metadata_spec.rb" with:
      """ruby
      require './helpers'

      RSpec.configure do |c|
        c.include Helpers, :include_helpers
        c.extend  Helpers, :extend_helpers
      end

      RSpec.describe "an example group with matching include metadata", :include_helpers do
        puts "In a group not matching the extend filter, help is #{help rescue 'not available'}"

        it "has access to the helper methods defined in the module" do
          expect(help).to be(:available)
        end
      end

      RSpec.describe "an example group with matching extend metadata", :extend_helpers do
        puts "In a group matching the extend filter, help is #{help}"

        it "does not have access to the helper methods defined in the module" do
          expect { help }.to raise_error(NameError)
        end
      end
      """
    When I run `rspec symbols_as_metadata_spec.rb`
    Then the examples should all pass
    And the output should contain "In a group not matching the extend filter, help is not available"
    And the output should contain "In a group matching the extend filter, help is available"