NUnit - ExpectedException with Custom Exception

While the NUnit docs give a good example of how to test for system exception types, it leaves you hanging on how to capture the custom exceptions that you are throwing. For instance, you might do something like:

throw new Exception("SSN should be formatted 123456789");

How do you set up your test attributes for this exception?

In VB:

<Test(), ExpectedException(GetType(Exception), "SSN should be formatted 123456789")>
        Public Sub ValidateSSNFormat()
        ' code that will cause the routine to throw the expected exception

In C#:

[Test]
[ExpectedException(typeof(Exception),"SSN should be formatted 123456789")]
public void ValidateSSNFormat()
// code that will cause the routine to throw the expected exception

No comments:

Post a Comment