Skip to content

Continuing on failure

Michael W. Powell edited this page Jun 20, 2024 · 3 revisions

When any step in a scenario fails, the default behaviour of xWellBehaved.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.

For instance, let us imagine that our calculator is some bizarre steampunk museum piece which tends to overheat. We may want to ensure that it does not 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.AssertNotNUll().Add(x, y));

    "Then the calculator temperature should remain below 100 degrees"
        .x(() => calculator.AssertTrue(x => x.Temperature < 100))
        .OnFailure(RemainingSteps.Run);
        //         ^^^^^^^^^^^^^^^^^^

    "And the answer is 3".x(() => answer.AssertEqual(3));
}
Clone this wiki locally