Skip to content

Exception assertions

Wesley Baartman edited this page May 30, 2019 · 4 revisions

Home > Assertions > Exception assertions

Introduction

This page will contain documentation for all assertions made about invocations.

Requirements

Using these assertions requires the following package to be installed in your project: AssertNet

Assertions

Invocations

DoesNotThrowException()

Description

Checks if an action does not throw an exception.

Examples
AssertThat(() => ExecuteMethod()).DoesNotThrowException();
DoesNotThrowException(T)

Description

Checks if an action does not throw an exception of the given type.

Examples
AssertThat(() => ExecuteMethod()).DoesNotThrowException(typeof(ArgumentException));
AssertThat(() => ExecuteMethod()).DoesNotThrowException<ArgumentException>();
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowException<Exception>(); // fails.
DoesNotThrowExactlyException(T)

Description

Checks if an action does not throw an exception of exactly the given type.

Examples
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowExactlyException(typeof(Exception)); // passes.
AssertThat(() => throw new ArgumentException(string.Empty)).DoesNotThrowExactlyException<Exception>();       // passes.
ThrowsException()

Description

Checks if an action does not throw an exception.

Examples
AssertThat(() => ExecuteMethod()).DoesNotThrowException();
ThrowsException(T)

Description

Checks if an action does throws an exception of the given type. This can also be subclass of the given type.

Examples
AssertThat(() => ExecuteMethod()).ThrowsException(typeof(ArgumentException));
AssertThat(() => ExecuteMethod()).ThrowsException<ArgumentException>();
AssertThat(() => throw new ArgumentException(string.Empty)).ThrowsException<Exception>(); // passes.
ThrowsExactlyException(T)

Description

Checks if an action does throws an exception of exactly the given type. This might not be a subclass of the given type.

Examples
AssertThat(() => throw new Exception()).ThrowsExactlyException(typeof(Exception));               // passes.
AssertThat(() => throw new Exception()).ThrowsExactlyException<Exception>();                     // passes.
AssertThat(() => throw new ArgumentException(string.Empty)).ThrowsExactlyException<Exception>(); // fails.

Exceptions

WithMessage(string)

Description

Checks if an exception has exactly the given message.

Examples
AssertThat(() => throw new Exception("hi")).ThrowsException().WithMessage("hi"); // passes.
AssertThat(() => throw new Exception()).ThrowsException().WithMessage("hi");     // fails.
WithMessageContaining(string)

Description

Checks if an exception has a message which contains the given substring.

Examples
AssertThat(() => throw new Exception("Hello world!")).ThrowsException().WithMessageContaining("world"); // passes.
WithNoInnerException()

Description

Checks if an exception has no inner exception.

Examples
AssertThat(() => throw new Exception()).ThrowsException().WithNoInnerException();                    // passes.
AssertThat(() => throw new Exception("", new Exception())).ThrowsException().WithNoInnerException(); // fails.
WithInnerException()

Description

Checks if an exception has an inner exception and continues asserting about the inner exception.

Examples
AssertThat(() => throw new Exception("", new Exception())).ThrowsException().WithInnerException(); // passes.
AssertThat(() => throw new Exception()).ThrowsException().WithInnerException();                  // fails.