-
Notifications
You must be signed in to change notification settings - Fork 0
Exception assertions
Home > Assertions > Exception assertions
This page will contain documentation for all assertions made about invocations.
Using these assertions requires the following package to be installed in your project: AssertNet
DoesNotThrowException()
Checks if an action does not throw an exception.
AssertThat(() => ExecuteMethod()).DoesNotThrowException();
DoesNotThrowException(T)
Checks if an action does not throw an exception of the given type.
AssertThat(() => ExecuteMethod()).DoesNotThrowException(typeof(ArgumentException));
AssertThat(() => ExecuteMethod()).DoesNotThrowException<ArgumentException>();
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowException<Exception>(); // fails.
DoesNotThrowExactlyException(T)
Checks if an action does not throw an exception of exactly the given type.
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowExactlyException(typeof(Exception)); // passes.
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowExactlyException<Exception>(); // passes.
ThrowsException()
Checks if an action does not throw an exception.
AssertThat(() => ExecuteMethod()).DoesNotThrowException();
ThrowsException(T)
Checks if an action does throws an exception of the given type. This can also be subclass of the given type.
AssertThat(() => ExecuteMethod()).ThrowsException(typeof(ArgumentException));
AssertThat(() => ExecuteMethod()).ThrowsException<ArgumentException>();
AssertThat(() => throw new ArgumentException(string.Empty)).ThrowsException<Exception>(); // passes.
ThrowsExactlyException(T)
Checks if an action does throws an exception of exactly the given type. This might not be a subclass of the given type.
AssertThat(() => throw new Exception()).ThrowsExactlyException(typeof(Exception)); // passes.
AssertThat(() => throw new Exception()).ThrowsExactlyException<Exception>(); // passes.
AssertThat(() => throw new ArgumentException(string.Empty)).ThrowsExactlyException<Exception>(); // fails.
WithMessage(string)
Checks if an exception has exactly the given message.
AssertThat(() => throw new Exception("hi")).ThrowsException().WithMessage("hi"); // passes.
AssertThat(() => throw new Exception()).ThrowsException().WithMessage("hi"); // fails.
WithMessageContaining(string)
Checks if an exception has a message which contains the given substring.
AssertThat(() => throw new Exception("Hello world!")).ThrowsException().WithMessageContaining("world"); // passes.
WithNoInnerException()
Checks if an exception has no inner exception.
AssertThat(() => throw new Exception()).ThrowsException().WithNoInnerException(); // passes.
AssertThat(() => throw new Exception("", new Exception())).ThrowsException().WithNoInnerException(); // fails.
WithInnerException()
Checks if an exception has an inner exception and continues asserting about the inner exception.
AssertThat(() => throw new Exception("", new Exception())).ThrowsException().WithInnerException(); // passes.
AssertThat(() => throw new Exception()).ThrowsException().WithInnerException(); // fails.