-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
world.feature
166 lines (149 loc) · 4.57 KB
/
world.feature
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Feature: World
In order to isolate each test scenario, the steps definitions belonging to the
same scenario will run in a isolated context, called 'world'. A world also
contains the helpers methods that will be used in the step definitions.
It is possible to add helpers methods to a world in three different ways:
1. Passing a module. In this case its methods will be added directly to the
world and be usable in the step definitions.
2. Passing a block, where the return value is the world object.
3. Passing a hash, where the keys are namespaces and the values are
modules. In this case, the methods of each module will be accessible using
the key as prefix.
Scenario: A world is extended using a module
Given a file named "features/support/helpers.rb" with:
"""
module Helpers
def helper_method
42
end
end
World(Helpers)
"""
And a file named "features/step_definitions/step.rb" with:
"""
Then /^the helper method is called$/ do
expect(helper_method).to eql(42)
end
"""
And a file named "features/f.feature" with:
"""
Feature: Calling a method
Scenario: I call a method without namespaces
Then the helper method is called
"""
When I run `cucumber features/f.feature`
Then it should pass
Scenario: A world is created using a block
Given a file named "features/support/helpers.rb" with:
"""
class Helper
def helper_method
42
end
end
World do
Helper.new
end
"""
And a file named "features/step_definitions/step.rb" with:
"""
Then /^the helper method is called$/ do
expect(helper_method).to eql(42)
end
"""
And a file named "features/f.feature" with:
"""
Feature: Calling a method
Scenario: I call a method from a namespace
Then the helper method is called
"""
When I run `cucumber features/f.feature`
Then it should pass
Scenario: A world is extended using a module with namespace
Given a file named "features/support/helpers.rb" with:
"""
module Helpers
def helper_method
42
end
end
World(my_namespace: Helpers)
"""
And a file named "features/step_definitions/step.rb" with:
"""
Then /^the helper method is called$/ do
expect(my_namespace.helper_method).to eql(42)
end
"""
And a file named "features/f.feature" with:
"""
Feature: Calling a method
Scenario: I call a method from a namespace
Then the helper method is called
"""
When I run `cucumber features/f.feature`
Then it should pass
Scenario: A world is existing over multiple scenarios
Given a file named "features/support/helpers.rb" with:
"""
module Helpers
def helper_method
42
end
end
World(my_namespace: Helpers)
"""
And a file named "features/step_definitions/step.rb" with:
"""
Then /^my_namespace is not nil$/ do
expect(my_namespace).not_to be_nil
end
Then /^the helper method is called$/ do
expect(my_namespace.helper_method).to eql(42)
end
"""
And a file named "features/f.feature" with:
"""
Feature: Calling a method
Scenario: The namespace exists
Then my_namespace is not nil
And the helper method is called
Scenario: The namespace still exists
Then my_namespace is not nil
And the helper method is called
"""
When I run `cucumber features/f.feature`
Then it should pass with:
"""
2 scenarios (2 passed)
"""
Scenario: A world is extended using multiple modules with different namespaces
Given a file named "features/support/helpers.rb" with:
"""
module ModuleOne
def forty_one
41
end
end
module ModuleTwo
def forty_two
42
end
end
World(module_one: ModuleOne, module_two: ModuleTwo)
"""
And a file named "features/step_definitions/step.rb" with:
"""
Then /^the helper method is called$/ do
expect(module_one.forty_one).to eql(41)
expect(module_two.forty_two).to eql(42)
end
"""
And a file named "features/f.feature" with:
"""
Feature: Calling a method
Scenario: I call a method from two namespaces
Then the helper method is called
"""
When I run `cucumber features/f.feature`
Then it should pass