Like for NUnit, AutoFixture offers a glue library for Moq.
By using this library, AutoFixture can use Moq to handle the requests for non-concrete types like abstract classes and interfaces. Optionally, AutoFixture can delegate to Moq the creation of fake delegates.
The AutoMoqCustomization is the core of the integration of AutoFixture with Moq. By adding an instance of this class to the fixture, requests for non-concrete types will be handled by Moq.
var fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization());
The snippets below are based on the following types
public class Service
{
public Service(IDependency dependency, AbstractType innerObject)
{
Dependency = dependency ?? throw new ArgumentNullException(nameof(dependency));
InnerObject = innerObject ?? throw new ArgumentNullException(nameof(innerObject));
}
public IDependency Dependency { get; }
public AbstractType InnerObject { get; }
}
public interface IDependency
{
void DoSomething(Action<string> action) { action("Hello world"); }
}
public abstract class AbstractType
{
public int IntValue { get; set; }
public string StringValue { get; set; }
}
Requesting objects
Once a fixture is enriched with AutoMoqCustomization, developers can use it to request anonymous instances of non-concrete types.
This can be done in these ways.
By requesting directly the type to be mocked. In this case an implicit mock will be returned.
By requesting for a mock of the type. In this case an instance of Mock<T> will be returned.
It is important to note that freezing is supported in both cases.
[Test]
public void Freezing_is_supported_when_requesting_type()
{
// ARRANGE
fixture.Customize(new AutoMoqCustomization());
var dependency = fixture.Freeze<IDependency>(); // freezing the type directly
// ACT
var sut = fixture.Create<Service>();
// ASSERT
Assert.That(sut.Dependency, Is.SameAs(dependency));
}
[Test]
public void Freezing_is_supported_when_requesting_Mock_of_type()
{
// ARRANGE
fixture.Customize(new AutoMoqCustomization());
var mockDependency = fixture.Freeze<Mock<IDependency>>(); // freezing a mock of the type
// ACT
var sut = fixture.Create<Service>();
// ASSERT
Assert.That(sut.Dependency, Is.SameAs(mockDependency.Object));
}
Mock configuration
Mocks requested by AutoFixture via AutoMoq have their configuration that differs from the normal defaults of Moq.
Specifically, here is the list of the properties of the mocks