AutoMocker class

June 26, 2021 · View on GitHub

An auto-mocking IoC container that generates mock objects using Moq.

public class AutoMocker

Public Members

namedescription
AutoMocker()Initializes an instance of AutoMockers.
AutoMocker(…)Initializes an instance of AutoMockers. (3 constructors)
CallBase { get; }Whether the base member virtual implementation will be called for created mocks if no setup is matched. Defaults to false.
DefaultValue { get; }Specifies the behavior to use when returning default values for unexpected invocations on loose mocks created by this instance.
MockBehavior { get; }Behavior of created mocks, according to the value set in the constructor.
ResolvedObjects { get; }A collection of objects stored in this AutoMocker instance. The keys are the types used when resolving services.
Resolvers { get; }A collection of resolves determining how a given dependency will be resolved.
AsDisposable()Retrieve an IDisposable instance that will dispose of all disposable instances contained within this AutoMocker instance.
Combine(…)Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemented by the same instance.
Combine<TService,TAsWellAs1>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5,TAsWellAs6>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5,TAsWellAs6,TAsWellAs7>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5,TAsWellAs6,TAsWellAs7,TAsWellAs8>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5,TAsWellAs6,TAsWellAs7,TAsWellAs8,TAsWellAs9>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
Combine<TService,TAsWellAs1,TAsWellAs2,TAsWellAs3,TAsWellAs4,TAsWellAs5,TAsWellAs6,TAsWellAs7,TAsWellAs8,TAsWellAs9,TAsWellAs10>()Combines all given types so that they are mocked by the same mock. Some IoC containers call this "Forwarding" one type to other interfaces. In the end, this just means that all given types will be implemnted by the same instance.
CreateInstance(…)Constructs an instance from known services. Any dependencies (constructor arguments) are fulfilled by searching the container or, if not found, automatically generating mocks. (2 methods)
CreateInstance<T>()Constructs an instance from known services. Any dependencies (constructor arguments) are fulfilled by searching the container or, if not found, automatically generating mocks.
CreateInstance<T>(…)Constructs an instance from known services. Any dependencies (constructor arguments) are fulfilled by searching the container or, if not found, automatically generating mocks.
CreateSelfMock<T>()Constructs a self-mock from the services available in the container. A self-mock is a concrete object that has virtual and abstract members mocked. The purpose is so that you can test the majority of a class but mock out a resource. This is great for testing abstract classes, or avoiding breaking cohesion even further with a non-abstract class.
CreateSelfMock<T>(…)Constructs a self-mock from the services available in the container. A self-mock is a concrete object that has virtual and abstract members mocked. The purpose is so that you can test the majority of a class but mock out a resource. This is great for testing abstract classes, or avoiding breaking cohesion even further with a non-abstract class.
Get(…)Searches and retrieves an object from the container that matches the serviceType. This can be a service setup explicitly via .Use() or implicitly with .CreateInstance(). (2 methods)
Get<TService>()Searches and retrieves an object from the container that matches TService. This can be a service setup explicitly via .Use() or implicitly with .CreateInstance().
Get<TService>(…)Searches and retrieves an object from the container that matches TService. This can be a service setup explicitly via .Use() or implicitly with .CreateInstance().
GetMock(…)Searches and retrieves the mock that the container uses for serviceType. (2 methods)
GetMock<TService>()Searches and retrieves the mock that the container uses for TService.
GetMock<TService>(…)Searches and retrieves the mock that the container uses for TService.
Setup<TService>(…)Shortcut for mock.Setup(...), creating the mock when necessary.
Setup<TService,TReturn>(…)Shortcut for mock.Setup(...), creating the mock when necessary. For specific return types. E.g. primitive, structs that cannot be inferred
SetupAllProperties<TService>()Shortcut for mock.SetupAllProperties(), creating the mock when necessary
SetupSequence<TService,TReturn>(…)Shortcut for mock.SetupSequence(), creating the mock when necessary
SetupWithAny<TService>(…)Specifies a setup on the mocked type for a call to a void method. All parameters are filled with IsAny according to the parameter's type.
SetupWithAny<TService,TReturn>(…)Specifies a setup on the mocked type for a call to a non-void (value-returning) method. All parameters are filled with IsAny according to the parameter's type.
Use(…)Adds an instance to the container.
Use<TService>(…)Adds an instance to the container. (3 methods)
Verify()This is a shortcut for calling mock.Verify() on every mock that we have.
Verify<T>()Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Moq.MockBehavior.Loose.
Verify<T>(…)Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Moq.MockBehavior.Loose. (11 methods)
Verify<T,TResult>(…)Verify a mock in the container. (5 methods)
VerifyAll()This is a shortcut for calling mock.VerifyAll() on every mock that we have.
With(…)Creates an instance of implementationType and registers it for service type serviceType. This is a convenience method for Use(serviceType, CreateInstance(implementationType)) (2 methods)
With<TImplementation>()Creates an instance of TImplementation and registers it as for service type TImplementation. This is a convenience method for Use<TImplementation>(CreateInstance<TImplementation>())
With<TService,TImplementation>()Creates an instance of TImplementation and registers it as for service type TService. This is a convenience method for Use<TService>(CreateInstance<TImplementation>())

See Also