Permalink
Browse files

Allow for asynchronous pending steps

  • Loading branch information...
1 parent 4a1f2f5 commit cdaa4ebbb3ac6509335f56448b36dc3c9fa38096 @jbpros jbpros committed Feb 5, 2012
@@ -0,0 +1,13 @@
+Feature: Asynchronous pending steps
+
+ Scenario: Pending step means the scenario is pending
+ Given a scenario with:
+ """
+ When I add 4 and 5
+ Then the result is 9
+ """
+ And the step "I add 4 and 5" has an asynchronous pending mapping
+ And the step "the result is 9" has a passing mapping
+ When Cucumber executes the scenario
+ Then the scenario is pending
+ And the step "the result is 9" is skipped
@@ -39,6 +39,10 @@ def write_pending_mapping(step_name)
append_step_definition(step_name, "callback.pending();")
end
+ def write_asynchronous_pending_mapping(step_name)
+ append_step_definition(step_name, "setTimeout(callback.pending, 10);")
+ end
+
def write_failing_mapping(step_name)
write_failing_mapping_with_message(step_name, "I was supposed to fail.")
end
@@ -51,6 +51,14 @@ var cucumberSteps = function() {
callback();
});
+ Given(/^the step "([^"]*)" has an asynchronous pending mapping$/, function(stepName, callback) {
+ this.stepDefinitions += "Given(/^" + stepName + "$/, function(callback) {\
+world.touchStep(\"" + stepName + "\");\
+setTimeout(callback.pending, 10);\
+});\n";
+ callback();
+ });
+
Given(/^the following feature:$/, function(feature, callback) {
this.featureSource = feature;
callback();
@@ -9,6 +9,10 @@
write_world_constructor_not_calling_back_with_instance
end
+Given /^the step "([^"]*)" has an asynchronous pending mapping$/ do |step_name|
+ write_asynchronous_pending_mapping(step_name)
+end
+
When /^Cucumber executes a scenario using that mapping$/ do
write_feature <<-EOF
Feature:
@@ -12,7 +12,8 @@ var StepDefinition = function(regexp, code) {
callback(successfulStepResult);
};
codeCallback.pending = function pending(reason) {
- throw Cucumber.Runtime.PendingStepException(reason);
+ var pendingStepResult = Cucumber.Runtime.PendingStepResult(reason);
+ callback(pendingStepResult);
};
var parameters = self.buildInvocationParameters(stepName, stepAttachment, codeCallback);
@@ -21,11 +22,7 @@ var StepDefinition = function(regexp, code) {
} catch (exception) {
if (exception)
Cucumber.Debug.warn(exception.stack || exception, 'exception inside feature', 3);
- var stepResult;
- if (exception instanceof Cucumber.Runtime.PendingStepException)
- stepResult = Cucumber.Runtime.PendingStepResult()
- else
- stepResult = Cucumber.Runtime.FailedStepResult(exception);
+ var stepResult = Cucumber.Runtime.FailedStepResult(exception);
callback(stepResult);
}
},
@@ -71,72 +71,42 @@ describe("Cucumber.SupportCode.StepDefinition", function() {
spyOn(Cucumber.Runtime, 'SuccessfulStepResult').andReturn(successfulStepResult);
});
- describe("when the step definition code executes successfully and calls back", function() {
- it("creates a successful step result", function() {
- codeExecutionCallback();
- expect(Cucumber.Runtime.SuccessfulStepResult).toHaveBeenCalled();
- });
+ it("creates a successful step result", function() {
+ codeExecutionCallback();
+ expect(Cucumber.Runtime.SuccessfulStepResult).toHaveBeenCalled();
+ });
- it("calls back", function() {
- codeExecutionCallback();
- expect(callback).toHaveBeenCalledWith(successfulStepResult);
- });
+ it("calls back", function() {
+ codeExecutionCallback();
+ expect(callback).toHaveBeenCalledWith(successfulStepResult);
+ });
- 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 claim its pendingness", function() {
+ expect(codeExecutionCallback.pending).toBeAFunction();
});
describe("pending()", function() {
- var pendingFunction, reason, exception;
+ var pendingReason, pendingStepResult;
beforeEach(function() {
- reason = createSpy("a reason for being pending");
- pendingFunction = codeExecutionCallback.pending;
- exception = createSpy("pending step exception");
- spyOn(Cucumber.Runtime, 'PendingStepException').andReturn(exception);
+ pendingReason = createSpy("pending reason");
+ pendingStepResult = createSpy("pending step result");
+ spyOn(Cucumber.Runtime, 'PendingStepResult').andReturn(pendingStepResult);
});
- it("creates a 'pending step' exception with the reason", function() {
- try { pendingFunction(reason); } catch (e) { }
- expect(Cucumber.Runtime.PendingStepException).toHaveBeenCalledWith(reason);
+ it("creates a pending step result", function() {
+ codeExecutionCallback.pending(pendingReason);
+ expect(Cucumber.Runtime.PendingStepResult).toHaveBeenCalledWith(pendingReason);
});
- it("throws the pending step exception", function() {
- expect(function() { pendingFunction(reason) }).toThrow(exception);
+ it("calls back", function() {
+ codeExecutionCallback.pending(pendingReason);
+ expect(callback).toHaveBeenCalledWith(pendingStepResult);
});
});
});
- describe("when the step calls back as pending", function() {
- var pendingReason, exception, pendingStepResult;
-
- beforeEach(function() {
- pendingReason = createSpy("a reason for being pending");
- pendingStepResult = createSpy("pending step result");
- exception = Cucumber.Runtime.PendingStepException(pendingReason);
- stepDefinitionCode.apply.andThrow(exception);
- spyOn(Cucumber.Runtime, 'PendingStepResult').andReturn(pendingStepResult);
- spyOn(Cucumber.Runtime, 'FailedStepResult');
- });
-
- it("creates a new pending step result", function() {
- stepDefinition.invoke(stepName, world, stepAttachment, callback);
- expect(Cucumber.Runtime.PendingStepResult).toHaveBeenCalled();
- });
-
- it("does not create a new failed step result", function() {
- stepDefinition.invoke(stepName, world, stepAttachment, callback);
- expect(Cucumber.Runtime.FailedStepResult).not.toHaveBeenCalled();
- });
-
- it("calls back with the step result", function() {
- stepDefinition.invoke(stepName, world, stepAttachment, callback);
- expect(callback).toHaveBeenCalledWith(pendingStepResult);
- });
- });
-
- describe("when the step definition code fails", function() {
+ describe("when the step definition code throws an exception", function() {
var failedStepResult, failureException;
beforeEach(function() {

0 comments on commit cdaa4eb

Please sign in to comment.