Testing Exception Messages with Shouldly

The Should.Throw() method returns the exception, so you can continue to test if for other things.

For example:

Should.Throw<MyException>(() => new ClassUnderTest().DoSomething())
    .Message.ShouldBe("My Custom Message");

Or, if you want to do more than just test the message:

MyException ex = Should.Throw<MyException>(() => new ClassUnderTest().DoSomething());

You can then do what you like with ex.

Note: Shouldly V2 uses Should.ThrowException<>()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)