sdu*_*ooy 45 unit-testing mstest xunit
我目前正将我的MsTest单元测试转换为xUnit.使用xUnit,有没有办法测试异常消息?测试异常消息是否正确,而不仅仅是异常类型?
the*_*ric 72
我认为测试Exception类型和消息都是正确的.在xUnit中都很容易:
var exception = Assert.Throws<AuthenticationException>(() => DoSomething());
Assert.Equal(message, exception.Message);
Run Code Online (Sandbox Code Playgroud)
最好使用 Record.Exception 方法,因为它匹配 AAA 模式:
[Fact]
public void Divide_TwoNumbers_ExpectException()
{
var sut = new Calculator();
var exception = Record.Exception(() => sut.Divide(10, 0));
Assert.IsType(typeof(DivideByZeroException), exception);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 ...