specflow中的异常步骤

时间:2018-03-19 20:24:05

标签: specflow

我在函数中调用step(specflow), 然后(步骤的字符串) 并在第一步抛出异常。我想用不同的函数捕获异常,而不是在步骤本身。有人知道怎么做吗? 感谢

1 个答案:

答案 0 :(得分:0)

SpecFlow无法做到这一点 SpecFlow将步骤中的异常解释为错误并停止执行Scenario。

您可以做的是捕获步骤中的异常并将其保存在绑定类的字段中。然后在第二步中,您可以检查此字段。

像这样:

[Binding]
public class BindingClass
{

    private Exception _exception;

    [When("an exception is thrown")
    public void ExceptionThrown()
    {
        try {
           .... //your code that throws an exception
        }
        catch(Exception e)
        {
            _exception = e;
        }
    }

    [Then("the exception has the message '(.*)'")]
    public void ExceptionHasTheMessage(string message)
    {
        if (_exception != null)
        {
            Assert.Equal(_exception.Message, message);
        }
    }
}
相关问题