bdd - 预期异常的 StoryQ 场景

标签 bdd storyq

我们如何使用 StoryQ 来测试预期出现异常的场景?

最佳答案

就实际代码而言,在测试代码的 .Then 部分,您需要创建一个 ActionFunc 来确定正在测试的内容,然后在代码的 .Then 部分,您将调用该代码并测试结果。例如:

[Test]
public void AnIllegalOperationThrowsAnException()
{
    new Story("My Story)
        .InOrderTo("Do achieve something")
        .AsA("User")
        .IWant("To carry out an operation")
        .WithScenario("an exception occurs")
        .Given(InitialConditions)
        .When(TheIllegalActionIsTaken)
        .Then(AnIllegalOperationExceptionIsThrown);
}

private void InitialConditions()
{
}

private Func<string> _operation;

private void TheIllegalActionIsTaken()
{
    _operation = () => return MyTestClass.DoesSomethingWrong();
}

private void AnIllegalOperationExceptionIsThrown()
{
    try
    {
        _operation.Invoke();
        Assert.Fail("An exception should have been thrown");
    }
    catch (Exception ex)
    {
        Assert.That(ex, Is.InstanceOf<IllegalOperationException>(), "The wrong exception was thrown");
        Assert.That(ex.Message, Is.EqualTo("Ooops!");
    }
}

断言处理可能会稍微整理一下,尤其是您的测试方法没有返回值。例如,FluentAssertions 库可以很好地与 Action(但不是 Func)一起工作,因此代码将是:

_action.ShouldThrow<IllegalOperationException>().WithMessage("Ooops!");

关于bdd - 预期异常的 StoryQ 场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275539/

相关文章:

javascript - 将 Jasmine 与 Sublime Text 2 结合使用

c# - 有人成功地将 SpecFlow 与 xUnit 2.0 一起使用了吗?

automated-tests - 使用SpecFlow进行端到端回归测试

c# - 有特定的用户故事场景是邪恶的吗?

java - Cucumber Java TestNG 的片段看起来很奇怪

c# - StoryQ When() 调用任务 C#