nUnit Assert.That(method,Throws.Exception) not catching exceptions

前端 未结 2 1256
天命终不由人
天命终不由人 2021-02-06 20:51

Can someone tell me why this unit test that checks for exceptions fails? Obviously my real test is checking other code but I\'m using Int32.Parse to show the issue.

<         


        
相关标签:
2条回答
  • 2021-02-06 21:45

    What test runner are you using? Not all of them work correctly with the exception assertions.

    You may have better luck using [ExpectedException (typeof(FormatException))] or even Assert.Throws<FormatException> (() => Int32.Parse("abc"));

    0 讨论(0)
  • 2021-02-06 21:47

    Try this instead:

    Assert.That(() => Int32.Parse("abc"), Throws.Exception.TypeOf<FormatException>());
    

    Basically you need to pass a delegate to Assert.That, just like the documentation in your link states (note that I've used a lambda expression here, but it should be the same).

    0 讨论(0)
提交回复
热议问题