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 =newFixture();fixture.Customize(newAutoMoqCustomization());
The snippets below are based on the following types
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]publicvoidFreezing_is_supported_when_requesting_type(){ // ARRANGEfixture.Customize(newAutoMoqCustomization());var dependency =fixture.Freeze<IDependency>(); // freezing the type directly // ACTvar sut =fixture.Create<Service>(); // ASSERTAssert.That(sut.Dependency,Is.SameAs(dependency));}[Test]publicvoidFreezing_is_supported_when_requesting_Mock_of_type(){ // ARRANGEfixture.Customize(newAutoMoqCustomization());var mockDependency =fixture.Freeze<Mock<IDependency>>(); // freezing a mock of the type // ACTvar sut =fixture.Create<Service>(); // ASSERTAssert.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