/
step_definition_spec.js
382 lines (309 loc) · 15.3 KB
/
step_definition_spec.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
require('../../support/spec_helper');
describe("Cucumber.SupportCode.StepDefinition", function () {
var Cucumber = requireLib('cucumber');
var stepDefinition, pattern, stepDefinitionCode;
beforeEach(function () {
pattern = createSpyWithStubs("pattern", {test: null});
stepDefinitionCode = createSpy("step definition code");
stepDefinition = Cucumber.SupportCode.StepDefinition(pattern, stepDefinitionCode);
spyOn(global, 'RegExp');
});
describe("getPatternRegexp()", function () {
describe("when the pattern is a RegExp", function () {
it("does not instantiate a RegExp", function () {
expect(global.RegExp).not.toHaveBeenCalled();
});
it("returns the pattern itself", function () {
expect(stepDefinition.getPatternRegexp()).toBe(pattern);
});
});
describe("when the pattern is a String", function () {
var regexp, quotedDollarParameterSubstitutedPattern, safeString, regexpString;
beforeEach(function () {
regexp = createSpy("regexp");
regexpString = "regexp string";
safeString = createSpyWithStubs("safe string");
quotedDollarParameterSubstitutedPattern = createSpyWithStubs("quoted dollar param substituted pattern", {replace: regexpString});
spyOnStub(pattern, 'replace').andReturn(safeString);
spyOnStub(safeString, 'replace').andReturn(quotedDollarParameterSubstitutedPattern);
global.RegExp.andReturn(regexp);
});
it("escapes unsafe regexp characters from the string", function () {
stepDefinition.getPatternRegexp();
expect(pattern.replace).toHaveBeenCalledWith(Cucumber.SupportCode.StepDefinition.UNSAFE_STRING_CHARACTERS_REGEXP, Cucumber.SupportCode.StepDefinition.PREVIOUS_REGEXP_MATCH);
});
it("replaces quoted dollar-prefixed parameters with the regexp equivalent", function () {
stepDefinition.getPatternRegexp();
expect(safeString.replace).toHaveBeenCalledWith(Cucumber.SupportCode.StepDefinition.QUOTED_DOLLAR_PARAMETER_REGEXP, Cucumber.SupportCode.StepDefinition.QUOTED_DOLLAR_PARAMETER_SUBSTITUTION);
});
it("replaces other dollar-prefixed parameter with the regexp equivalent", function () {
stepDefinition.getPatternRegexp();
expect(quotedDollarParameterSubstitutedPattern.replace).toHaveBeenCalledWith(Cucumber.SupportCode.StepDefinition.DOLLAR_PARAMETER_REGEXP, Cucumber.SupportCode.StepDefinition.DOLLAR_PARAMETER_SUBSTITUTION);
});
it("instantiates a new RegExp", function () {
stepDefinition.getPatternRegexp();
expect(global.RegExp).toHaveBeenCalledWith("^" + regexpString + "$");
});
it("returns the new RegExp", function () {
expect(stepDefinition.getPatternRegexp()).toBe(regexp);
});
});
});
describe("matchesStepName()", function () {
var patternRegexp, stepName;
beforeEach(function () {
stepName = createSpy("step name");
matchResult = createSpy("step match result (boolean)");
patternRegexp = createSpyWithStubs("pattern regexp", {test: matchResult});
spyOn(stepDefinition, 'getPatternRegexp').andReturn(patternRegexp);
});
it("gets the pattern regexp", function (){
stepDefinition.matchesStepName(stepName);
expect(stepDefinition.getPatternRegexp).toHaveBeenCalled();
});
it("tests the string against the step name", function () {
stepDefinition.matchesStepName(stepName);
expect(patternRegexp.test).toHaveBeenCalledWith(stepName);
});
it("returns the match result", function () {
expect(stepDefinition.matchesStepName(stepName)).toBe(matchResult);
});
});
describe("invoke()", function () {
var step, world, callback;
var parameters, exceptionHandler;
beforeEach(function () {
step = createSpy("step");
world = createSpy("world");
callback = createSpy("callback");
parameters = createSpy("code execution parameters");
exceptionHandler = createSpy("exception handler");
spyOn(Cucumber.Util.Exception, 'registerUncaughtExceptionHandler');
spyOn(stepDefinition, 'buildInvocationParameters').andReturn(parameters);
spyOn(stepDefinition, 'buildExceptionHandlerToCodeCallback').andReturn(exceptionHandler);
spyOn(stepDefinitionCode, 'apply');
});
it("builds the step invocation parameters", function () {
stepDefinition.invoke(step, world, callback);
expect(stepDefinition.buildInvocationParameters).toHaveBeenCalled();
expect(stepDefinition.buildInvocationParameters).toHaveBeenCalledWithValueAsNthParameter(step, 1);
expect(stepDefinition.buildInvocationParameters).toHaveBeenCalledWithAFunctionAsNthParameter(2);
});
it("builds an exception handler for the code callback", function () {
stepDefinition.invoke(step, world, callback);
expect(stepDefinition.buildExceptionHandlerToCodeCallback).toHaveBeenCalledWithAFunctionAsNthParameter(1);
var codeExecutionCallbackPassedToParameterBuilder = stepDefinition.buildInvocationParameters.mostRecentCall.args[1];
var codeExecutionCallbackPassedToExceptionHandlerBuilder = stepDefinition.buildExceptionHandlerToCodeCallback.mostRecentCall.args[0];
expect(codeExecutionCallbackPassedToExceptionHandlerBuilder).toBe(codeExecutionCallbackPassedToParameterBuilder);
});
it("registers the exception handler for uncaught exceptions", function () {
stepDefinition.invoke(step, world, callback);
expect(Cucumber.Util.Exception.registerUncaughtExceptionHandler).toHaveBeenCalledWith(exceptionHandler);
});
it("calls the step definition code with the parameters and World as 'this'", function () {
stepDefinition.invoke(step, world, callback);
expect(stepDefinitionCode.apply).toHaveBeenCalledWith(world, parameters);
});
describe("callback passed to the step definition code", function () {
var codeExecutionCallback;
var successfulStepResult;
beforeEach(function () {
stepDefinition.invoke(step, world, callback);
codeExecutionCallback = stepDefinition.buildInvocationParameters.mostRecentCall.args[1];
successfulStepResult = createSpy("successful step result");
spyOn(Cucumber.Runtime, 'SuccessfulStepResult').andReturn(successfulStepResult);
spyOn(Cucumber.Util.Exception, 'unregisterUncaughtExceptionHandler');
});
describe("when called without an error", function () {
beforeEach(function () {
spyOn(codeExecutionCallback, 'fail');
codeExecutionCallback();
});
it("creates a successful step result", function () {
expect(Cucumber.Runtime.SuccessfulStepResult).toHaveBeenCalledWith({step: step});
});
it("unregisters the exception handler", function () {
expect(Cucumber.Util.Exception.unregisterUncaughtExceptionHandler).toHaveBeenCalledWith(exceptionHandler);
});
it("calls back", function () {
expect(callback).toHaveBeenCalledWith(successfulStepResult);
});
it("does not fail", function () {
expect(codeExecutionCallback.fail).not.toHaveBeenCalled();
});
});
describe("when called with an error", function () {
var error;
beforeEach(function () {
error = createSpy("error");
spyOn(codeExecutionCallback, 'fail');
codeExecutionCallback(error);
});
it("does not create a successful step result", function () {
expect(Cucumber.Runtime.SuccessfulStepResult).not.toHaveBeenCalled();
});
it("does not unregister the exception handler", function () {
expect(Cucumber.Util.Exception.unregisterUncaughtExceptionHandler).not.toHaveBeenCalled();
});
it("does not call back", function () {
expect(callback).not.toHaveBeenCalled();
});
it("fails", function () {
expect(codeExecutionCallback.fail).toHaveBeenCalledWith(error);
});
});
it("supplies a function to the step to let it claim its pendingness", function () {
expect(codeExecutionCallback.pending).toBeAFunction ();
});
it("supplies a function to the step to let it fail asynchronously", function () {
expect(codeExecutionCallback.fail).toBeAFunction ();
});
describe("pending()", function () {
var pendingReason, pendingStepResult;
beforeEach(function () {
pendingReason = createSpy("pending reason");
pendingStepResult = createSpy("pending step result");
spyOn(Cucumber.Runtime, 'PendingStepResult').andReturn(pendingStepResult);
});
it("creates a pending step result", function () {
codeExecutionCallback.pending(pendingReason);
expect(Cucumber.Runtime.PendingStepResult).toHaveBeenCalledWith({step: step, pendingReason: pendingReason});
});
it("unregisters the exception handler", function () {
codeExecutionCallback.pending(pendingReason);
expect(Cucumber.Util.Exception.unregisterUncaughtExceptionHandler).toHaveBeenCalledWith(exceptionHandler);
});
it("calls back", function () {
codeExecutionCallback.pending(pendingReason);
expect(callback).toHaveBeenCalledWith(pendingStepResult);
});
});
describe("fail()", function () {
var failureReason, failedStepResult;
beforeEach(function () {
failureReason = createSpy("failure reason");
failedStepResult = createSpy("failed step result");
spyOn(Cucumber.Runtime, 'FailedStepResult').andReturn(failedStepResult);
});
it("creates a failing step result", function () {
codeExecutionCallback.fail(failureReason);
expect(Cucumber.Runtime.FailedStepResult).toHaveBeenCalledWith({step: step, failureException: failureReason});
});
describe("when no failure reason is given", function () {
it("creates a failing step result with a generic step failure exception", function () {
codeExecutionCallback.fail();
var payload = Cucumber.Runtime.FailedStepResult.mostRecentCall.args[0];
expect(payload.step).toBe(step);
expect(payload.failureException).toBeAnInstanceOf(Error);
});
});
it("unregisters the exception handler", function () {
codeExecutionCallback.fail(failureReason);
expect(Cucumber.Util.Exception.unregisterUncaughtExceptionHandler).toHaveBeenCalledWith(exceptionHandler);
});
it("calls back", function () {
codeExecutionCallback.fail(failureReason);
expect(callback).toHaveBeenCalledWith(failedStepResult);
});
});
});
describe("when the step definition code throws an exception", function () {
var failureException;
beforeEach(function () {
failureException = createSpy("I am a failing step definition");
stepDefinitionCode.apply.andThrow(failureException);
});
it("handles the exception with the exception handler", function () {
stepDefinition.invoke(step, world, callback);
expect(exceptionHandler).toHaveBeenCalledWith(failureException);
});
});
});
describe("buildInvocationParameters()", function () {
var patternRegexp, step, stepName, stepAttachment, stepAttachmentContents;
var matches, callback;
beforeEach(function () {
stepName = createSpy("step name to match");
matches = createSpyWithStubs("matches", {shift: null, push: null});
patternRegexp = createSpyWithStubs("pattern regexp", {test: matches});
stepAttachmentContents = createSpy("step attachment contents");
step = createSpyWithStubs("step", {hasAttachment: null, getName: stepName, getAttachmentContents: stepAttachmentContents});
callback = createSpy("callback");
spyOn(stepDefinition, 'getPatternRegexp').andReturn(patternRegexp);
spyOnStub(patternRegexp, 'exec').andReturn(matches);
});
it("gets the step name", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(step.getName).toHaveBeenCalled();
});
it("gets the pattern regexp", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(stepDefinition.getPatternRegexp).toHaveBeenCalled();
});
it("executes the pattern regexp against the step name", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(patternRegexp.exec).toHaveBeenCalledWith(stepName);
});
it("removes the whole matched string of the regexp result array (to only keep matching groups)", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(matches.shift).toHaveBeenCalled();
});
it("checks whether the step has an attachment or not", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(step.hasAttachment).toHaveBeenCalled();
});
describe("when the step has an attachment", function () {
beforeEach(function () {
step.hasAttachment.andReturn(true);
});
it("gets the attachment contents", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(step.getAttachmentContents).toHaveBeenCalled();
});
it("adds the attachment contents to the parameter array", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(matches.push).toHaveBeenCalledWith(stepAttachmentContents);
});
});
describe("when the step has no attachment", function () {
beforeEach(function () {
step.hasAttachment.andReturn(false);
});
it("does not get the attachment contents", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(step.getAttachmentContents).not.toHaveBeenCalled();
});
it("does not add the attachement contents to the parameter array", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(matches.push.callCount).toBe(1);
});
});
it("adds the callback to the parameter array", function () {
stepDefinition.buildInvocationParameters(step, callback);
expect(matches.push).toHaveBeenCalledWith(callback);
});
it("returns the parameters", function () {
expect(stepDefinition.buildInvocationParameters(step, callback)).toBe(matches);
});
});
describe("buildExceptionHandlerToCodeCallback()", function () {
var codeCallback, exceptionHandler;
beforeEach(function () {
codeCallback = createSpyWithStubs("code callback", {fail: null});
exceptionHandler = stepDefinition.buildExceptionHandlerToCodeCallback(codeCallback);
});
it("returns an exception handler", function () {
expect(exceptionHandler).toBeAFunction ();
});
describe("returned exception handler", function () {
var exception;
beforeEach(function () {
exception = createSpy("exception");
});
it("calls back as a failure with the exception", function () {
exceptionHandler(exception);
expect(codeCallback.fail).toHaveBeenCalledWith(exception);
});
});
});
});