Continuing on failure

Adam Ralph edited this page Apr 15, 2016 · 4 revisions
Clone this wiki locally

When any step in a scenario fails, the default behaviour of xBehave.net is to skip all remaining steps. However, there may be cases when you want to continue the execution of remaining steps when a specific step fails. This can be done very easily using the OnFailure() extension method.

E.g. let us imagine that our calculator is some bizarre steampunk museum piece which tends to overheat. We may want to ensure that it doesn't overheat when adding two numbers together, but in the case it does, we are still interested in getting the right answer. If the calculator does overheat, the scenario will still fail, since the step that tests the temperature will fail, but the subsequent step which tests the answer will still be run so that we can still inspect the outcome of that assertion.

[Scenario]
public void Addition(int x, int y, Calculator calculator, int answer)
{
    "Given the number 1"
        .x(() => x = 1);

    "And the number 2"
        .x(() => y = 2);

    "And a calculator"
        .x(() => calculator = new Calculator());

    "When I add the numbers together"
        .x(() => answer = calculator.Add(x, y));

    "Then the calculator temperature should remain below 100 degrees"
        .x(() => calculator.Temperature.Should().BeLessThan(100))
        .OnFailure(RemainingSteps.Run);

    "And the answer is 3"
        .x(() => answer.Should().Be(3));
}