Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  1. Moq

Exceptions

One of the most common tasks that were solved with callbacks is throwing an exception when a certain method is invoked with a given set of parameters.

var mock = new Mock<IService>();

mock.Setup(p => p.DoSomething())
    .Callback(() => throw new Exception());

To streamline the configuration of methods throwing exceptions, Moq has special constructs targeting this scenario: Throws and ThrowsAsync.

Both methods accept an instance of any Exception type.

mock.Setup(p => p.DoSomething())
    .Throws(new Exception("My custom exception"));

mock.Setup(p => p.DoSomethingAsync())
    .ThrowsAsync(new Exception("My custom exception"));

Additionally, Throws can also instantiate an exception given its type.

mock.Setup(p => p.DoSomething())
    .Throws<Exception>();

Thows and ThrowsAsync can also be used in a sequence of calls.

mock.SetupSequence(p => p.GetSomeValue())
    .Returns(1)
    .Throws<Exception>();

mock.SetupSequence(p => p.GetSomeValueAsync())
    .ReturnsAsync(1)
    .ThrowsAsync(new Exception());

Throwing exceptions aware of incoming parameters

Unfortunately, Throws and ThrowsAsync have no overload accepting a delegate to be lazily evaluated.

The lack of such overloads makes it impossible to throw exceptions that are somehow aware of the incoming parameters.

The following statement is not supported.

mock.Setup(p => p.DoSomething(It.IsAny<int>()))
    .Throws((int value) => new Exception($"{value} is not valid"));

The same behavior can be modeled with callbacks.

mock.Setup(p => p.DoSomething(It.IsAny<int>()))
    .Callback((int value) => throw new Exception($"{value} is not valid"));
PreviousCallbacksNextEvents

Last updated 4 years ago