Sparky's Tool Tips: Moq.AutoMock
Tags: csharp, dotnet, testing, productivity
Published: 2021-07-26 01:09:27

Probably the most important thing that makes .NET classes unit-testable is [dependency injection](https://stackify.com/dependency-injection-c-sharp/) (injecting [interfaces, not implementations](https://betterprogramming.pub/code-against-interfaces-not-implementations-37b30e7ab992)), usually via constructor injection.

Mocking libraries make it easier to generate "mock" interface implementations, and to verify that the interfaces were used as expected. [Moq](https://github.com/moq/moq4) is the most popular .NET mocking library. 

> *If you're using Moq, you may be interested in my [SparkyTestHelpers.Moq and SparkyTestHelpers.MoqFluent](https://dev.to/sparky/sparkytesthelpers-moq-307h) NuGet packages. They provide an alternative to "It.IsAny" and "It.Is" syntax that I think is easier to write and read, and [fluent assertions](https://fluentassertions.com/) for verifications.*

....lost of interfaces

The [Moq.AutoMock NuGet package](https://www.nuget.org/packages/Moq.AutoMock/) provides an "auto-mocker" container for Moq.

AutoMocker.CreateInstance<*T*> creates a new instance of the specified type, automatically generating Mocks for all of its constructor interface parameters. (The class must have a constructor where *all* of the parameters are interfaces):

Without AutoMocker:
```csharp
var mockReader = new Mock<IReader>();
var mockFormatter = new Mock<IFormatter>();
var mockLogger = new Mock<ILogger>();
var foo = new Foo(
    mockReader.Object, mockFormatter.Object, mockLogger.Object);
```
With AutoMocker:
```csharp
var autoMocker = new AutoMocker();
var foo = autoMocker.CreateInstance<Foo>();
```

To **.Setup** or **.Verify** mock behavior, you can get the auto-generated mock via AutoMocker's "GetMock<*TMock*>" method:

```csharp
autoMocker.GetMock<IFormatter>.Setup(x =>
    x.Format(It.IsAny<decimal>()).Returns(x => x.ToString("###.00"));
```

You may find it advantageous to create a custom AutoMocker subclass for your project that sets up "happy path" behavior for some of your commonly used interfaces:

```csharp
public class CustomAutoMocker : AutoMocker
{
    // constructor
    public CustomAutoMocker()
    {
        // Set up commonly used interfaces
        Mock<IFormatter> mockFormatter = GetMock<IFormatter>();
        //mockFormatter.Setup(x => ...
    }
}
```
```csharp
    var autoMocker = new CustomAutoMocker();
    var foo autoMocker.CreateInstance<Foo>();
    // following tests will use Setups defined in the CustomAutoMocker
```

A custom AutoMocker subclass can also override the **CreateInstance<*T*>** method to perform additional logic when an instance of a specific type is created. For example, in a project using **ApiController**s that I work on (which was not originally written with unit testing in mind), I detect construction of **ApiController** instances and automatically set up their **.Request** and **.User** properties:

```csharp
public class CustomAutoMocker : AutoMocker
...
public new T CreateInstance<T>() where T : class
{
    T instance = base.CreateInstance<T>();

    var apiController = instance as ApiController;
    if (apiController != null)
    {
        // Set up ApiController's Request and User 
        apiController.Request = new HttpRequestMessage();
        apiController.Request.SetConfiguration(new HttpConfiguration());
        apiController.User = BuildClaimsPrincipal();
    }

    return instance;
}

private ClaimsPrincipal BuildClaimsPrincipal()
{
    // (code to build ClaimsPrincipal and
    // assign it to Thread.CurrentPrincipal)
}
```
- - -
**Mock.AutoMock** and a custom **AutoMocker** subclass have greatly reduced the amount of repetitive and tedious mock setup code I need to write in my unit tests.

If you're using **Moq**, I highly recommend it.