About auto-mocking

SpecsFor includes an auto-mocking container. What's that, you ask? It's a special Inversion of Control container that resolves dependencies using mock objects.

At test-time, when SpecsFor creates your System Under Test (SUT), it does so using its auto-mocking container. Any dependencies that your SUT has are resolved (recursively) using the container. If a dependency is mockable, meaning it is either an abstract type or or an interface type, the container will create a new mock object using the Moq framework, then it will inject that mock object for you automatically.

Conceptually, you can think of the auto-mocking container as doing the following for you:

SUT = new YourTargetClass(new Mock<IService1>().Object, new Mock<IService2>().Object);

This mocking occurs recursively, too. If your class depends on a concrete type, the container will create an instance of the concrete type. If that concrete type has a dependency on a mockable type though, it will inject mock objects for those dependencies.

You can retrieve any mock object that has been created by the auto-mocking container by calling the GetMockFor method on the SpecsFor base class.

For more information check out how to set up a mock object and how to verify a method was called. You can also reconfigure the behavior of the auto-mocking container, such as registering a concrete type.


Did this page help you?