Mid From PDF Testing Unit 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:

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

myService.DoSomethingInvalidAsync());

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details