@@ -11,31 +11,24 @@ describe("Cucumber.SupportCode.Library", function() {
11
11
12
12
beforeEach ( function ( ) {
13
13
rawSupportCode = createSpy ( "Raw support code" ) ;
14
- afterHookCollection = Cucumber . Type . Collection ( ) ;
15
- beforeHookCollection = Cucumber . Type . Collection ( ) ;
14
+ beforeHookCollection = createSpy ( "before hook collection" ) ;
15
+ afterHookCollection = createSpy ( "after hook collection" ) ;
16
16
stepDefinitionCollection = [
17
17
createSpyWithStubs ( "First step definition" , { matchesStepName :false } ) ,
18
18
createSpyWithStubs ( "Second step definition" , { matchesStepName :false } ) ,
19
19
createSpyWithStubs ( "Third step definition" , { matchesStepName :false } )
20
20
] ;
21
21
worldConstructor = createSpy ( "world constructor" ) ;
22
22
spyOnStub ( stepDefinitionCollection , 'syncForEach' ) . andCallFake ( function ( cb ) { stepDefinitionCollection . forEach ( cb ) ; } ) ;
23
- spyOn ( Cucumber . Type , 'Collection' ) . andCallFake ( function ( ) {
24
- if ( this . Collection . callCount == 1 ) {
25
- return beforeHookCollection ;
26
- } else if ( this . Collection . callCount == 2 ) {
27
- return afterHookCollection ;
28
- } else {
29
- return stepDefinitionCollection ;
30
- }
31
- } ) ;
23
+ spyOn ( Cucumber . Type , 'Collection' ) . andReturnSeveral ( [ beforeHookCollection , afterHookCollection , stepDefinitionCollection ] ) ;
32
24
spyOn ( Cucumber . SupportCode , 'WorldConstructor' ) . andReturn ( worldConstructor ) ;
33
25
library = Cucumber . SupportCode . Library ( rawSupportCode ) ;
34
26
} ) ;
35
27
36
28
describe ( "constructor" , function ( ) {
37
- it ( "creates a collection of step definitions" , function ( ) {
29
+ it ( "creates collecitons of before hooks, after hooks and step definitions" , function ( ) {
38
30
expect ( Cucumber . Type . Collection ) . toHaveBeenCalled ( ) ;
31
+ expect ( Cucumber . Type . Collection . callCount ) . toBe ( 3 ) ;
39
32
} ) ;
40
33
41
34
it ( "executes the raw support code" , function ( ) {
@@ -57,12 +50,12 @@ describe("Cucumber.SupportCode.Library", function() {
57
50
supportCodeHelper = rawSupportCode . mostRecentCall . object ;
58
51
} ) ;
59
52
60
- it ( "exposes a method to define Before methods " , function ( ) {
53
+ it ( "exposes a method to define Before hooks " , function ( ) {
61
54
expect ( supportCodeHelper . Before ) . toBeAFunction ( ) ;
62
55
expect ( supportCodeHelper . Before ) . toBe ( library . defineBeforeHook ) ;
63
56
} ) ;
64
57
65
- it ( "exposes a method to define After methods " , function ( ) {
58
+ it ( "exposes a method to define After hooks " , function ( ) {
66
59
expect ( supportCodeHelper . After ) . toBeAFunction ( ) ;
67
60
expect ( supportCodeHelper . After ) . toBe ( library . defineAfterHook ) ;
68
61
} ) ;
@@ -97,7 +90,6 @@ describe("Cucumber.SupportCode.Library", function() {
97
90
var stepName ;
98
91
99
92
beforeEach ( function ( ) {
100
- library = Cucumber . SupportCode . Library ( rawSupportCode ) ;
101
93
stepName = createSpy ( "Step name" ) ;
102
94
} ) ;
103
95
@@ -174,36 +166,40 @@ describe("Cucumber.SupportCode.Library", function() {
174
166
} ) ;
175
167
176
168
describe ( "triggerBeforeHooks" , function ( ) {
177
- var beforeHook , callback , code , invokeSpy , world ;
169
+ var world , callback ;
178
170
179
171
beforeEach ( function ( ) {
180
- code = createSpy ( "before code" ) ;
181
- world = library . instantiateNewWorld ( ) ;
172
+ world = createSpy ( "world" ) ;
182
173
callback = createSpy ( "callback" ) ;
183
- beforeHook = createSpy ( "before hook" ) ;
184
- invokeSpy = spyOnStub ( beforeHook , "invoke" ) ;
185
- spyOn ( Cucumber . SupportCode , "Hook" ) . andReturn ( beforeHook ) ;
186
- library . defineBeforeHook ( code ) ;
174
+ spyOnStub ( beforeHookCollection , 'forEach' ) ;
187
175
} ) ;
188
176
189
- it ( "triggers each before hook" , function ( ) {
190
- library . triggerBeforeHooks ( world , function ( ) {
191
- expect ( beforeHook , "invoke" ) .
192
- toHaveBeenCalledWithValueAsNthParameter ( world , 1 ) ;
193
- expect ( beforeHook , "invoke" ) .
194
- toHaveBeenCalledWithAFunctionAsNthParameter ( 2 ) ;
195
- } ) ;
177
+ it ( "iterates over the before hooks" , function ( ) {
178
+ library . triggerBeforeHooks ( world , callback ) ;
179
+ expect ( beforeHookCollection . forEach ) . toHaveBeenCalled ( ) ;
180
+ expect ( beforeHookCollection . forEach ) . toHaveBeenCalledWithAFunctionAsNthParameter ( 1 ) ;
181
+ expect ( beforeHookCollection . forEach ) . toHaveBeenCalledWithValueAsNthParameter ( callback , 2 ) ;
196
182
} ) ;
197
183
198
- it ( "calls the callback when finished" , function ( ) {
199
- invokeSpy . andCallFake ( function ( world , callback ) { callback ( ) ; } ) ;
200
- library . triggerBeforeHooks ( world , callback ) ;
201
- expect ( callback ) . toHaveBeenCalled ( ) ;
184
+ describe ( "for each before hook" , function ( ) {
185
+ var beforeHook , forEachBeforeHookFunction , forEachBeforeHookFunctionCallback ;
186
+
187
+ beforeEach ( function ( ) {
188
+ library . triggerBeforeHooks ( world , callback ) ;
189
+ forEachBeforeHookFunction = beforeHookCollection . forEach . mostRecentCall . args [ 0 ] ;
190
+ forEachBeforeHookFunctionCallback = createSpy ( "for each before hook iteration callback" ) ;
191
+ beforeHook = createSpyWithStubs ( "before hook" , { invoke : null } ) ;
192
+ } ) ;
193
+
194
+ it ( "invokes the hook" , function ( ) {
195
+ forEachBeforeHookFunction ( beforeHook , forEachBeforeHookFunctionCallback ) ;
196
+ expect ( beforeHook . invoke ) . toHaveBeenCalledWith ( world , forEachBeforeHookFunctionCallback ) ;
197
+ } ) ;
202
198
} ) ;
203
199
} ) ;
204
200
205
201
describe ( "defineAfterHook" , function ( ) {
206
- var code , afterHook ;
202
+ var afterHook , code ;
207
203
208
204
beforeEach ( function ( ) {
209
205
code = createSpy ( "after code" ) ;
@@ -217,38 +213,42 @@ describe("Cucumber.SupportCode.Library", function() {
217
213
expect ( Cucumber . SupportCode . Hook ) . toHaveBeenCalledWith ( code ) ;
218
214
} ) ;
219
215
220
- it ( "unshifts the after hook to the after hooks collection" , function ( ) {
216
+ it ( "adds the after hook to the after hooks collection" , function ( ) {
221
217
library . defineAfterHook ( code ) ;
222
218
expect ( afterHookCollection . unshift ) . toHaveBeenCalledWith ( afterHook ) ;
223
219
} ) ;
224
220
} ) ;
225
221
226
222
describe ( "triggerAfterHooks" , function ( ) {
227
- var afterHook , callback , code , invokeSpy , world ;
223
+ var world , callback ;
228
224
229
225
beforeEach ( function ( ) {
230
- code = createSpy ( "after code" ) ;
231
- world = library . instantiateNewWorld ( ) ;
232
- callback = createSpy ( "callback" ) ;
233
- afterHook = createSpy ( "after hook" ) ;
234
- invokeSpy = spyOnStub ( afterHook , "invoke" ) ;
235
- spyOn ( Cucumber . SupportCode , "Hook" ) . andReturn ( afterHook ) ;
236
- library . defineAfterHook ( code ) ;
226
+ world = createSpy ( "world" ) ;
227
+ callback = createSpy ( "callback" ) ;
228
+ spyOnStub ( afterHookCollection , 'forEach' ) ;
237
229
} ) ;
238
230
239
- it ( "triggers each after hook" , function ( ) {
240
- library . triggerAfterHooks ( world , function ( ) {
241
- expect ( afterHook , "invoke" ) .
242
- toHaveBeenCalledWithValueAsNthParameter ( world , 1 ) ;
243
- expect ( afterHook , "invoke" ) .
244
- toHaveBeenCalledWithAFunctionAsNthParameter ( 2 ) ;
245
- } ) ;
231
+ it ( "iterates over the after hooks" , function ( ) {
232
+ library . triggerAfterHooks ( world , callback ) ;
233
+ expect ( afterHookCollection . forEach ) . toHaveBeenCalled ( ) ;
234
+ expect ( afterHookCollection . forEach ) . toHaveBeenCalledWithAFunctionAsNthParameter ( 1 ) ;
235
+ expect ( afterHookCollection . forEach ) . toHaveBeenCalledWithValueAsNthParameter ( callback , 2 ) ;
246
236
} ) ;
247
237
248
- it ( "calls the callback when finished" , function ( ) {
249
- invokeSpy . andCallFake ( function ( world , callback ) { callback ( ) ; } ) ;
250
- library . triggerAfterHooks ( world , callback ) ;
251
- expect ( callback ) . toHaveBeenCalled ( ) ;
238
+ describe ( "for each after hook" , function ( ) {
239
+ var afterHook , forEachAfterHookFunction , forEachAfterHookFunctionCallback ;
240
+
241
+ beforeEach ( function ( ) {
242
+ library . triggerAfterHooks ( world , callback ) ;
243
+ forEachAfterHookFunction = afterHookCollection . forEach . mostRecentCall . args [ 0 ] ;
244
+ forEachAfterHookFunctionCallback = createSpy ( "for each after hook iteration callback" ) ;
245
+ afterHook = createSpyWithStubs ( "after hook" , { invoke : null } ) ;
246
+ } ) ;
247
+
248
+ it ( "invokes the hook" , function ( ) {
249
+ forEachAfterHookFunction ( afterHook , forEachAfterHookFunctionCallback ) ;
250
+ expect ( afterHook . invoke ) . toHaveBeenCalledWith ( world , forEachAfterHookFunctionCallback ) ;
251
+ } ) ;
252
252
} ) ;
253
253
} ) ;
254
254
0 commit comments