Class Mock<T>
- Namespace
- Moq
- Assembly
- Moq.dll
Provides a mock implementation of T.
public class Mock<T> : Mock, IMock<T> where T : class
Type Parameters
TType to mock, which can be an interface, a class, or a delegate.
- Inheritance
-
Mock<T>
- Implements
-
IMock<T>
- Inherited Members
Examples
The following example shows establishing setups with specific values for method invocations:
// Arrange
var order = new Order(TALISKER, 50);
var warehouse = new Mock<IWarehouse>();
warehouse.Setup(w => w.HasInventory(TALISKER, 50)).Returns(true);
// Act
order.Fill(warehouse.Object);
// Assert
Assert.True(order.IsFilled);
The following example shows how to use the It class to specify conditions for arguments instead of specific values:
// Arrange
var order = new Order(TALISKER, 50);
var warehouse = new Mock<IWarehouse>();
// shows how to expect a value within a range:
warehouse.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsInRange(0, 100, Range.Inclusive)))
.Returns(false);
// shows how to throw for unexpected calls.
warehouse.Setup(x => x.Remove(
It.IsAny<string>(),
It.IsAny<int>()))
.Throws(new InvalidOperationException());
// Act
order.Fill(warehouse.Object);
// Assert
Assert.False(order.IsFilled);
Remarks
Any interface type can be used for mocking, but for classes, only abstract and virtual members can be mocked.
The behavior of the mock with regards to the setups and the actual calls is determined by the optional MockBehavior that can be passed to the Mock(MockBehavior) constructor.
Constructors
Mock()
Initializes an instance of the mock with Default behavior.
public Mock()
Examples
var mock = new Mock<IFormatProvider>();
Mock(MockBehavior)
Initializes an instance of the mock with the specified MockBehavior behavior.
public Mock(MockBehavior behavior)
Parameters
behaviorMockBehaviorBehavior of the mock.
Examples
var mock = new Mock<IFormatProvider>(MockBehavior.Strict);
Mock(MockBehavior, params object[])
Initializes an instance of the mock with a specific MockBehavior behavior and with the given constructor arguments for the class.
public Mock(MockBehavior behavior, params object[] args)
Parameters
behaviorMockBehaviorBehavior of the mock.
argsobject[]Optional constructor arguments if the mocked type is a class.
Remarks
The mock will try to find the best match constructor given the constructor arguments, and invoke that to initialize the instance. This applies only to classes, not interfaces.
Mock(Expression<Func<T>>, MockBehavior)
Initializes an instance of the mock using the given constructor call including its argument values and with a specific MockBehavior behavior.
public Mock(Expression<Func<T>> newExpression, MockBehavior behavior = MockBehavior.Default)
Parameters
newExpressionExpression<Func<T>>Lambda expression that creates an instance of
T.behaviorMockBehaviorBehavior of the mock.
Examples
var mock = new Mock<MyProvider>(() => new MyProvider(someArgument, 25), MockBehavior.Loose);
Mock(params object[])
Initializes an instance of the mock with Default behavior
and with the given constructor arguments for the class. (Only valid when T is a class.)
public Mock(params object[] args)
Parameters
argsobject[]Optional constructor arguments if the mocked type is a class.
Examples
var mock = new Mock<MyProvider>(someArgument, 25);
Remarks
The mock will try to find the best match constructor given the constructor arguments, and invoke that to initialize the instance.This applies only for classes, not interfaces.
Properties
Behavior
public override MockBehavior Behavior { get; }
Property Value
CallBase
public override bool CallBase { 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 override DefaultValueProvider DefaultValueProvider { get; set; }
Property Value
Name
Allows naming of your mocks, so they can be easily identified in error messages (e.g. from failed assertions).
public string Name { get; set; }
Property Value
Object
Exposes the mocked object instance.
public virtual T Object { get; }
Property Value
- T
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 override Switches Switches { get; set; }
Property Value
Methods
As<TInterface>()
Adds an interface implementation to the mock, allowing setups to be specified for it.
public override Mock<TInterface> As<TInterface>() where TInterface : class
Returns
- Mock<TInterface>
Type Parameters
TInterfaceType of interface to cast the mock to.
Examples
The following example creates a mock for the main interface and later adds IDisposable to it to verify it's called by the consumer code:
var mock = new Mock<IProcessor>();
mock.Setup(x => x.Execute("ping"));
// add IDisposable interface
var disposable = mock.As<IDisposable>();
disposable.Setup(d => d.Dispose())
.Verifiable();
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.
OnGetObject()
Returns the mocked object value.
protected override object OnGetObject()
Returns
Raise(Action<T>, EventArgs)
Raises the event referenced in eventExpression using the given args argument.
public void Raise(Action<T> eventExpression, EventArgs args)
Parameters
Examples
The following example shows how to raise a PropertyChanged event:
var mock = new Mock<IViewModel>();
mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));
This example shows how to invoke an event with a custom event arguments class in a view that will cause its corresponding presenter to react by changing its state:
var mockView = new Mock<IOrdersView>();
var presenter = new OrdersPresenter(mockView.Object);
// Check that the presenter has no selection by default
Assert.Null(presenter.SelectedOrder);
// Raise the event with a specific arguments data
mockView.Raise(v => v.SelectionChanged += null, new OrderEventArgs { Order = new Order("moq", 500) });
// Now the presenter reacted to the event, and we have a selected order
Assert.NotNull(presenter.SelectedOrder);
Assert.Equal("moq", presenter.SelectedOrder.ProductName);
Exceptions
- ArgumentException
The
argsargument is invalid for the target event invocation, or theeventExpressionis not an event attach or detach expression.
Raise(Action<T>, params object[])
Raises the event referenced in eventExpression using the given args argument for a non-EventHandler-typed event.
public void Raise(Action<T> eventExpression, params object[] args)
Parameters
Examples
The following example shows how to raise a custom event that does not adhere
to the standard EventHandler:
var mock = new Mock<IViewModel>();
mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);
Exceptions
- ArgumentException
The
argsarguments are invalid for the target event invocation, or theeventExpressionis not an event attach or detach expression.
Setup(Expression<Action<T>>)
Specifies a setup on the mocked type for a call to a void method.
public ISetup<T> Setup(Expression<Action<T>> expression)
Parameters
expressionExpression<Action<T>>Lambda expression that specifies the expected method invocation.
Returns
- ISetup<T>
Examples
var mock = new Mock<IProcessor>();
mock.Setup(x => x.Execute("ping"));
Remarks
If more than one setup is specified for the same method or property, the latest one wins and is the one that will be executed.
SetupAdd(Action<T>)
Specifies a setup on the mocked type for a call to an event add.
public ISetup<T> SetupAdd(Action<T> addExpression)
Parameters
addExpressionAction<T>Lambda expression that adds an event.
Returns
- ISetup<T>
Examples
mock.SetupAdd(x => x.EventHandler += (s, e) => {});
Remarks
If more than one setup is set for the same event add, the latest one wins and is the one that will be executed.
SetupAllProperties()
Specifies that the all properties on the mock should have "property behavior", meaning that setting their value will cause them to be saved and later returned when the properties is requested. (This is also known as "stubbing".) The default value for each property will be the one generated as specified by the DefaultValue property for the mock.
public Mock<T> SetupAllProperties()
Returns
- Mock<T>
Remarks
If the mock's DefaultValue is set to Mock, the mocked default values will also get all properties setup recursively.
SetupGet<TProperty>(Expression<Func<T, TProperty>>)
Specifies a setup on the mocked type for a call to a property getter.
public ISetupGetter<T, TProperty> SetupGet<TProperty>(Expression<Func<T, TProperty>> expression)
Parameters
expressionExpression<Func<T, TProperty>>Lambda expression that specifies the property getter.
Returns
- ISetupGetter<T, TProperty>
Type Parameters
TPropertyType of the property. Typically omitted as it can be inferred from the expression.
Examples
mock.SetupGet(x => x.Suspended)
.Returns(true);
Remarks
If more than one setup is set for the same property getter, the latest one wins and is the one that will be executed.
SetupProperty<TProperty>(Expression<Func<T, TProperty>>)
Specifies that the given property should have "property behavior", meaning that setting its value will cause it to be saved and later returned when the property is requested. (This is also known as "stubbing".)
public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property)
Parameters
propertyExpression<Func<T, TProperty>>Property expression to stub.
Returns
- Mock<T>
Type Parameters
TPropertyType of the property, inferred from the property expression (does not need to be specified).
Examples
If you have an interface with an int property Value,
you might stub it using the following straightforward call:
var mock = new Mock<IHaveValue>();
mock.SetupProperty(v => v.Value);
After the SetupProperty call has been issued, setting and retrieving
the object value will behave as expected:
IHaveValue v = mock.Object;
v.Value = 5;
Assert.Equal(5, v.Value);
SetupProperty<TProperty>(Expression<Func<T, TProperty>>, TProperty)
Specifies that the given property should have "property behavior", meaning that setting its value will cause it to be saved and later returned when the property is requested. This overload allows setting the initial value for the property. (This is also known as "stubbing".)
public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property, TProperty initialValue)
Parameters
propertyExpression<Func<T, TProperty>>Property expression to stub.
initialValueTPropertyInitial value for the property.
Returns
- Mock<T>
Type Parameters
TPropertyType of the property, inferred from the property expression (does not need to be specified).
Examples
If you have an interface with an int property Value,
you might stub it using the following straightforward call:
var mock = new Mock<IHaveValue>();
mock.SetupProperty(v => v.Value, 5);
After the SetupProperty call has been issued, setting and retrieving the object value
will behave as expected:
IHaveValue v = mock.Object;
Assert.Equal(5, v.Value); // Initial value was stored
// New value set which changes the initial value
v.Value = 6;
Assert.Equal(6, v.Value);
SetupRemove(Action<T>)
Specifies a setup on the mocked type for a call to an event remove.
public ISetup<T> SetupRemove(Action<T> removeExpression)
Parameters
removeExpressionAction<T>Lambda expression that removes an event.
Returns
- ISetup<T>
Examples
mock.SetupRemove(x => x.EventHandler -= (s, e) => {});
Remarks
If more than one setup is set for the same event remove, the latest one wins and is the one that will be executed.
SetupSequence(Expression<Action<T>>)
Performs a sequence of actions, one per call.
public ISetupSequentialAction SetupSequence(Expression<Action<T>> expression)
Parameters
expressionExpression<Action<T>>
Returns
SetupSequence<TResult>(Expression<Func<T, TResult>>)
Return a sequence of values, once per call.
public ISetupSequentialResult<TResult> SetupSequence<TResult>(Expression<Func<T, TResult>> expression)
Parameters
expressionExpression<Func<T, TResult>>
Returns
- ISetupSequentialResult<TResult>
Type Parameters
TResult
SetupSet(Action<T>)
Specifies a setup on the mocked type for a call to a property setter.
public ISetup<T> SetupSet(Action<T> setterExpression)
Parameters
setterExpressionAction<T>Lambda expression that sets a property to a value.
Returns
- ISetup<T>
Examples
mock.SetupSet(x => x.Suspended = true);
Remarks
If more than one setup is set for the same property setter, the latest one wins and is the one that will be executed.
SetupSet<TProperty>(Action<T>)
Specifies a setup on the mocked type for a call to a property setter.
public ISetupSetter<T, TProperty> SetupSet<TProperty>(Action<T> setterExpression)
Parameters
setterExpressionAction<T>The Lambda expression that sets a property to a value.
Returns
- ISetupSetter<T, TProperty>
Type Parameters
TPropertyType of the property.
Examples
mock.SetupSet(x => x.Suspended = true);
Remarks
If more than one setup is set for the same property setter, the latest one wins and is the one that will be executed.
This overloads allows the use of a callback already typed for the property type.
Setup<TResult>(Expression<Func<T, TResult>>)
Specifies a setup on the mocked type for a call to a non-void (value-returning) method.
public ISetup<T, TResult> Setup<TResult>(Expression<Func<T, TResult>> expression)
Parameters
expressionExpression<Func<T, TResult>>Lambda expression that specifies the method invocation.
Returns
- ISetup<T, TResult>
Type Parameters
TResultType of the return value. Typically omitted as it can be inferred from the expression.
Examples
mock.Setup(x => x.HasInventory("Talisker", 50))
.Returns(true);
Remarks
If more than one setup is specified for the same method or property, the latest one wins and is the one that will be executed.
ToString()
Returns the name of the mock.
public override string ToString()
Returns
Verify(Expression<Action<T>>)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression)
Parameters
expressionExpression<Action<T>>Expression to verify.
Examples
This example assumes that the mock has been used, and later we want to verify that a given invocation with specific parameters was performed:
var mock = new Mock<IProcessor>();
... // exercise mock
// Will throw if the test code didn't call Execute with a "ping" string argument.
mock.Verify(proc => proc.Execute("ping"));
Exceptions
- MockException
The invocation was not performed on the mock.
Verify(Expression<Action<T>>, Times)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression, Times times)
Parameters
expressionExpression<Action<T>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify(Expression<Action<T>>, Times, string)
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression, Times times, string failMessage)
Parameters
expressionExpression<Action<T>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify(Expression<Action<T>>, Func<Times>)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression, Func<Times> times)
Parameters
expressionExpression<Action<T>>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify(Expression<Action<T>>, Func<Times>, string)
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression, Func<Times> times, string failMessage)
Parameters
expressionExpression<Action<T>>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify(Expression<Action<T>>, string)
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjunction with the default Loose.
public void Verify(Expression<Action<T>> expression, string failMessage)
Parameters
expressionExpression<Action<T>>Expression to verify.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyAdd(Action<T>)
Verifies that an event was added to the mock.
public void VerifyAdd(Action<T> addExpression)
Parameters
addExpressionAction<T>Expression to verify.
Examples
This example assumes that the mock has been used, and later we want to verify that a given event handler was subscribed to an event:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't subscribe to the OnClosed event.
mock.VerifyAdd(warehouse => warehouse.OnClosed += It.IsAny<EventHandler>());
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyAdd(Action<T>, Times)
Verifies that an event was added to the mock.
public void VerifyAdd(Action<T> addExpression, Times times)
Parameters
addExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyAdd(Action<T>, Times, string)
Verifies that an event was added to the mock, specifying a failure message.
public void VerifyAdd(Action<T> addExpression, Times times, string failMessage)
Parameters
addExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyAdd(Action<T>, Func<Times>)
Verifies that an event was added to the mock.
public void VerifyAdd(Action<T> addExpression, Func<Times> times)
Parameters
addExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyAdd(Action<T>, Func<Times>, string)
Verifies that an event was added to the mock, specifying a failure message.
public void VerifyAdd(Action<T> addExpression, Func<Times> times, string failMessage)
Parameters
addExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyAdd(Action<T>, string)
Verifies that an event was added to the mock, specifying a failure message.
public void VerifyAdd(Action<T> addExpression, string failMessage)
Parameters
addExpressionAction<T>Expression to verify.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>)
Verifies that a property was read on the mock.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Examples
This example assumes that the mock has been used, and later we want to verify that a given property was retrieved from it:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't retrieve the IsClosed property.
mock.VerifyGet(warehouse => warehouse.IsClosed);
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>, Times)
Verifies that a property was read on the mock.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression, Times times)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Exceptions
- MockException
The invocation was not called the number times specified by
times.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>, Times, string)
Verifies that a property was read on the mock, specifying a failure error message.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression, Times times, string failMessage)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Exceptions
- MockException
The invocation was not called the number times specified by
times.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>, Func<Times>)
Verifies that a property was read on the mock.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression, Func<Times> times)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Exceptions
- MockException
The invocation was not called the number times specified by
times.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>, Func<Times>, string)
Verifies that a property was read on the mock, specifying a failure error message.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression, Func<Times> times, string failMessage)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Exceptions
- MockException
The invocation was not called the number times specified by
times.
VerifyGet<TProperty>(Expression<Func<T, TProperty>>, string)
Verifies that a property was read on the mock, specifying a failure error message.
public void VerifyGet<TProperty>(Expression<Func<T, TProperty>> expression, string failMessage)
Parameters
expressionExpression<Func<T, TProperty>>Expression to verify.
failMessagestringMessage to show if verification fails.
Type Parameters
TPropertyType of the property to verify. Typically omitted as it can be inferred from the expression's return type.
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyNoOtherCalls()
Verifies that there were no calls other than those already verified.
public void VerifyNoOtherCalls()
Exceptions
- MockException
There was at least one invocation not previously verified.
VerifyRemove(Action<T>)
Verifies that an event was removed from the mock.
public void VerifyRemove(Action<T> removeExpression)
Parameters
removeExpressionAction<T>Expression to verify.
Examples
This example assumes that the mock has been used, and later we want to verify that a given event handler was removed from an event:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't unsubscribe from the OnClosed event.
mock.VerifyRemove(warehouse => warehouse.OnClose -= It.IsAny<EventHandler>());
Exceptions
- MockException
The invocation was not performed on the mock.
VerifyRemove(Action<T>, Times)
Verifies that an event was removed from the mock.
public void VerifyRemove(Action<T> removeExpression, Times times)
Parameters
removeExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyRemove(Action<T>, Times, string)
Verifies that an event was removed from the mock, specifying a failure message.
public void VerifyRemove(Action<T> removeExpression, Times times, string failMessage)
Parameters
removeExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyRemove(Action<T>, Func<Times>)
Verifies that an event was removed from the mock.
public void VerifyRemove(Action<T> removeExpression, Func<Times> times)
Parameters
removeExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyRemove(Action<T>, Func<Times>, string)
Verifies that an event was removed from the mock, specifying a failure message.
public void VerifyRemove(Action<T> removeExpression, Func<Times> times, string failMessage)
Parameters
removeExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifyRemove(Action<T>, string)
Verifies that an event was removed from the mock, specifying a failure message.
public void VerifyRemove(Action<T> removeExpression, string failMessage)
Parameters
removeExpressionAction<T>Expression to verify.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not performed on the mock.
VerifySet(Action<T>)
Verifies that a property was set on the mock.
public void VerifySet(Action<T> setterExpression)
Parameters
setterExpressionAction<T>Expression to verify.
Examples
This example assumes that the mock has been used, and later we want to verify that a given property was set on it:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed = true);
Exceptions
- MockException
The invocation was not performed on the mock.
VerifySet(Action<T>, Times)
Verifies that a property was set on the mock.
public void VerifySet(Action<T> setterExpression, Times times)
Parameters
setterExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifySet(Action<T>, Times, string)
Verifies that a property was set on the mock, specifying a failure message.
public void VerifySet(Action<T> setterExpression, Times times, string failMessage)
Parameters
setterExpressionAction<T>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifySet(Action<T>, Func<Times>)
Verifies that a property was set on the mock.
public void VerifySet(Action<T> setterExpression, Func<Times> times)
Parameters
setterExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifySet(Action<T>, Func<Times>, string)
Verifies that a property was set on the mock, specifying a failure message.
public void VerifySet(Action<T> setterExpression, Func<Times> times, string failMessage)
Parameters
setterExpressionAction<T>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
VerifySet(Action<T>, string)
Verifies that a property was set on the mock, specifying a failure message.
public void VerifySet(Action<T> setterExpression, string failMessage)
Parameters
setterExpressionAction<T>Expression to verify.
failMessagestringMessage to show if verification fails.
Examples
This example assumes that the mock has been used, and later we want to verify that a given property was set on it:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed = true,
"Warehouse should always be closed after the action");
Exceptions
- MockException
The invocation was not performed on the mock.
Verify<TResult>(Expression<Func<T, TResult>>)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify<TResult>(Expression<Func<T, TResult>> expression)
Parameters
expressionExpression<Func<T, TResult>>Expression to verify.
Type Parameters
TResultType of return value from the expression.
Examples
This example assumes that the mock has been used, and later we want to verify that a given invocation with specific parameters was performed:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't call HasInventory.
mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50));
Exceptions
- MockException
The invocation was not performed on the mock.
Verify<TResult>(Expression<Func<T, TResult>>, Times)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify<TResult>(Expression<Func<T, TResult>> expression, Times times)
Parameters
expressionExpression<Func<T, TResult>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
Type Parameters
TResultType of return value from the expression.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify<TResult>(Expression<Func<T, TResult>>, Times, string)
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.
public void Verify<TResult>(Expression<Func<T, TResult>> expression, Times times, string failMessage)
Parameters
expressionExpression<Func<T, TResult>>Expression to verify.
timesTimesThe number of times a method is expected to be called.
failMessagestringMessage to show if verification fails.
Type Parameters
TResultType of return value from the expression.
Exceptions
- MockException
The invocation was not called the number times specified by
times.
Verify<TResult>(Expression<Func<T, TResult>>, Func<Times>)
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjunction with the default Loose.
public void Verify<TResult>(Expression<Func<T, TResult>> expression, Func<Times> times)
Parameters
expressionExpression<Func<T, TResult>>Expression to verify.
timesFunc<Times>The number of times a method is expected to be called.
Type Parameters
TResultType of return value from the expression.
Exceptions
- MockException
The invocation was not called the number of times specified by
times.
Verify<TResult>(Expression<Func<T, TResult>>, string)
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.
public void Verify<TResult>(Expression<Func<T, TResult>> expression, string failMessage)
Parameters
expressionExpression<Func<T, TResult>>Expression to verify.
failMessagestringMessage to show if verification fails.
Type Parameters
TResultType of return value from the expression.
Examples
This example assumes that the mock has been used, and later we want to verify that a given invocation with specific parameters was performed:
var mock = new Mock<IWarehouse>();
... // exercise mock
// Will throw if the test code didn't call HasInventory.
mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50),
"When filling orders, inventory has to be checked");
Exceptions
- MockException
The invocation was not performed on the mock.
When(Func<bool>)
Allows setting up a conditional setup. Conditional setups are only matched by an invocation when the specified condition evaluates to true at the time when the invocation occurs.
public ISetupConditionResult<T> When(Func<bool> condition)
Parameters
conditionFunc<bool>The condition that should be checked when a setup is being matched against an invocation.