Mid Testing

How do you test exception handling in unit tests?

Use assertion methods that expect exceptions. For example, in MSTest:

[TestMethod]

[ExpectedException(typeof(InvalidOperationException))]

public void Method_ShouldThrowException()

myService.DoSomethingInvalid();

Or in xUnit:

await Assert.ThrowsAsync<InvalidOperationException>(() =>

myService.DoSomethingInvalidAsync());

More from C# Programming Tutorial

All questions for this course