# Combining AutoFixture with NUnit and Moq

Using the glue libraries to combine AutoFixture with [NUnit](/unit-testing-csharp/autofixture/nunit-glue-library.md) and [Moq](/unit-testing-csharp/autofixture/moq-glue-library.md) gives the possibility to write very powerful and semantic-rich unit tests.

To combine these libraries, it's necessary to create a [custom version of the `AutoData` attribute](/unit-testing-csharp/autofixture/nunit-glue-library.md#customizing-the-fixture) that customizes a fixture with the `AutoMoqCustomization`.

```csharp
[AttributeUsage(AttributeTargets.Method)]
public class CustomAutoDataAttribute : AutoDataAttribute
{
    public CustomAutoDataAttribute() : base (CreateFixture) {}

    private IFixture CreateFixture()
    {
        var fixture = new Fixture();

        fixture.Customize(new AutoMoqCustomization { ConfigureMembers = true, GenerateDelegates = true });

        return fixture;
    }
}
```

The `CustomAutoDataAttribute` attribute above shows how to customize a fixture by adding an instance of `AutoMoqCustomization`.

Let's consider the following service as an example

```csharp
public interface IDependency
{
    void SendMessage(string message);
}

public class Service
{
    private readonly IDependency _dependency;
    private readonly ILogger<Service> _logger;

    public Service (IDependency dependency, ILogger<Service> logger)
    {
        _dependency = dependency ?? throw new ArgumentNullException(nameof(dependency));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public void DoSomething(string message)
    {
        _dependency.SendMessage(message);
    }
}
```

Here is a unit test testing that, when invoking `DoSomething`, the message is forwarded to the `SendMessage` method of the `IDependency`

```csharp
[Test]
public void DoSomething_forwards_message_to_Dependency()
{
    // ARRANGE
    var fixture = new Fixture();
    var mockDependency = new Mock<IDependency>();
    var message = fixture.Create<string>();

    var sut = new Service(mockDependency.Object, Mock.Of<ILogger<IDependency>>());

    // ACT
    sut.DoSomething(message);

    // ASSERT
    mockDependency.Verify(p => p.SendMessage(message), Times.Once());
}
```

The same test can be rewritten using the `CustomAutoDataAttribute` shown above

```csharp
[Test, CustomAutoDataAttribute]
public void DoSomething_forwards_message_to_Dependency([Frozen] IDependency dependency, Service sut, string message)
{
    // ACT
    sut.DoSomething(message);

    // ASSERT
    Mock.Get(dependency).Verify(p => p.SendMessage(message), Times.Once());
}
```

The snippet above shows how well AutoFixture, NUnit and Moq integrate with each other. This is clearly shown by the frozen parameter that gets fed with an implicit mock. `[Frozen] IDependency dependency`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.insightarchitectures.com/unit-testing-csharp/autofixture/combining-autofixture-with-nunit-and-moq.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
