Class Mock
- Namespace
- Moq
- Assembly
- Moq.dll
Base class for mocks and static helper class with methods that apply to mocked objects, such as Get<T>(T) to retrieve a Mock<T> from an object instance.
public abstract class Mock
- Inheritance
-
Mock
- Derived
-
Mock<T>
- Inherited Members
Constructors
Mock()
Initializes a new instance of the Mock class.
protected Mock()
Properties
Behavior
Behavior of the mock, according to the value set in the constructor.
public abstract MockBehavior Behavior { get; }
Property Value
CallBase
Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to false.
public abstract bool CallBase { get; set; }
Property Value
DefaultValue
Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.
public DefaultValue DefaultValue { get; set; }
Property Value
DefaultValueProvider
Gets or sets the DefaultValueProvider instance that will be used e. g. to produce default return values for unexpected invocations.
public abstract DefaultValueProvider DefaultValueProvider { get; set; }
Property Value
Invocations
Gets list of invocations which have been performed on this mock.
public IInvocationList Invocations { get; }
Property Value
Object
Gets the mocked object instance.
public object Object { get; }
Property Value
Setups
Gets the setups that have been configured on this mock, in chronological order (that is, oldest setup first, most recent setup last).
public ISetupList Setups { get; }
Property Value
Switches
A set of switches that influence how this mock will operate. You can opt in or out of certain features via this property.
public abstract Switches Switches { get; set; }
Property Value
Methods
As<TInterface>()
Adds an interface implementation to the mock, allowing setups to be specified for it.
public abstract Mock<TInterface> As<TInterface>() where TInterface : class
Returns
- Mock<TInterface>
Type Parameters
TInterfaceType of interface to cast the mock to.
Remarks
This method can only be called before the first use of the mock Object property, at which point the runtime type has already been generated and no more interfaces can be added to it.
Also, TInterface must be an interface and not a class,
which must be specified when creating the mock instead.
Exceptions
- ArgumentException
The
TInterfacespecified is not an interface.- InvalidOperationException
The mock type has already been generated by accessing the Object property.
Get<T>(T)
Retrieves the mock object for the given object instance.
public static Mock<T> Get<T>(T mocked) where T : class
Parameters
mockedTThe instance of the mocked object.
Returns
- Mock<T>
The mock associated with the mocked object.
Type Parameters
TType of the mock to retrieve. Can be omitted as it's inferred from the object instance passed in as the
mockedinstance.
Examples
The following example shows how to add a new setup to an object instance which is not the original Mock<T> but rather the object associated with it:
// Typed instance, not the mock, is retrieved from some test API.
HttpContextBase context = GetMockContext();
// context.Request is the typed object from the "real" API
// so in order to add a setup to it, we need to get
// the mock that "owns" it
Mock<HttpRequestBase> request = Mock.Get(context.Request);
request.Setup(req => req.AppRelativeCurrentExecutionFilePath)
.Returns(tempUrl);
Exceptions
- ArgumentException
The received
mockedinstance was not created by Moq.
Of<T>()
Creates a mock object of the indicated type.
public static T Of<T>() where T : class
Returns
- T
The mocked object created.
Type Parameters
TThe type of the mocked object.
Of<T>(MockBehavior)
Creates a mock object of the indicated type.
public static T Of<T>(MockBehavior behavior) where T : class
Parameters
behaviorMockBehaviorBehavior of the mock.
Returns
- T
The mocked object created.
Type Parameters
TThe type of the mocked object.
Of<T>(Expression<Func<T, bool>>)
Creates a mock object of the indicated type.
public static T Of<T>(Expression<Func<T, bool>> predicate) where T : class
Parameters
predicateExpression<Func<T, bool>>The predicate with the specification of how the mocked object should behave.
Returns
- T
The mocked object created.
Type Parameters
TThe type of the mocked object.
Of<T>(Expression<Func<T, bool>>, MockBehavior)
Creates a mock object of the indicated type.
public static T Of<T>(Expression<Func<T, bool>> predicate, MockBehavior behavior) where T : class
Parameters
predicateExpression<Func<T, bool>>The predicate with the specification of how the mocked object should behave.
behaviorMockBehaviorBehavior of the mock.
Returns
- T
The mocked object created.
Type Parameters
TThe type of the mocked object.
OnGetObject()
Returns the mocked object value.
protected abstract object OnGetObject()
Returns
SetReturnsDefault<TReturn>(TReturn)
Defines the default return value for all mocked methods or properties with return type TReturn.
public void SetReturnsDefault<TReturn>(TReturn value)
Parameters
valueTReturnThe default return value.
Type Parameters
TReturnThe return type for which to define a default value.
Remarks
Default return value is respected only when there is no matching setup for a method call.
Verify()
Verifies that all verifiable expectations have been met.
public void Verify()
Examples
This example sets up an expectation and marks it as verifiable.
After the mock is used, a Verify() call is issued on the mock
to ensure the method in the setup was invoked:
var mock = new Mock<IWarehouse>();
this.Setup(x => x.HasInventory(TALISKER, 50))
.Returns(true)
.Verifiable();
...
// Will throw if the test code did not call HasInventory.
this.Verify();
Exceptions
- MockException
Not all verifiable expectations were met.
Verify(params Mock[])
Verifies that all verifiable expectations have been met.
public static void Verify(params Mock[] mocks)
Parameters
mocksMock[]
Exceptions
- MockException
Not all verifiable expectations were met.
VerifyAll()
Verifies all expectations regardless of whether they have been flagged as verifiable.
public void VerifyAll()
Examples
This example sets up an expectation without marking it as verifiable. After the mock is used, a VerifyAll(params Mock[]) call is issued on the mock to ensure that all expectations are met:
var mock = new Mock<IWarehouse>();
this.Setup(x => x.HasInventory(TALISKER, 50))
.Returns(true);
...
// Will throw if the test code did not call HasInventory,
// even though that expectation was not marked as verifiable.
mock.VerifyAll();
Exceptions
- MockException
At least one expectation was not met.
VerifyAll(params Mock[])
Verifies all expectations regardless of whether they have been flagged as verifiable.
public static void VerifyAll(params Mock[] mocks)
Parameters
mocksMock[]
Exceptions
- MockException
At least one expectation was not met.