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

Posted by JasonM on Stack Overflow See other posts from Stack Overflow or by JasonM
Published on 2010-03-25T15:04:18Z Indexed on 2010/03/25 15:13 UTC
Read the original article Hit count: 207

Filed under:
|
|
|

Hi Everyone,

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.

[Test]
public void MyTest()
{
    Assert.That(Int32.Parse("abc"), Throws.Exception.TypeOf<FormatException>());
}

The test fails, giving this error. Obviously I'm trying to test for this exception and I think I'm missing something in my syntax.

Error   1   TestCase '.MyTest'
failed: System.FormatException : Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)

based on the documentation at Throws Constraint (NUnit 2.5)

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET