Class Match
- Namespace
- Moq
- Assembly
- Moq.dll
Allows creating custom value matchers that can be used on setups and verification, completely replacing the built-in It class with your own argument matching rules.
public abstract class Match
- Inheritance
-
Match
- Derived
- Inherited Members
Examples
Creating a custom matcher is straightforward. You just need to create a method that returns a value from a call to Create<T>(Predicate<T>) with your matching condition and optional friendly render expression:
public Order IsBigOrder()
{
return Match.Create<Order>(
o => o.GrandTotal >= 5000,
() => IsBigOrder()); // a friendly expression to render on failures
}
This method can be used in any mock setup invocation:
mock.Setup(m => m.Submit(IsBigOrder())
.Throws<UnauthorizedAccessException>();
At runtime, Moq knows that the return value was a matcher and evaluates your predicate with the actual value passed into your predicate.
Another example might be a case where you want to match a lists of orders that contains a particular one. You might create matcher like the following:
public static class Orders
{
public static IEnumerable<Order> Contains(Order order)
{
return Match.Create<IEnumerable<Order>>(orders => orders.Contains(order));
}
}
Now we can invoke this static method instead of an argument in an invocation:
var order = new Order { ... };
var mock = new Mock<IRepository<Order>>();
mock.Setup(x => x.Save(Orders.Contains(order)))
.Throws<ArgumentException>();
Remarks
Argument matching is used to determine whether a concrete invocation in the mock matches a given setup. This matching mechanism is fully extensible.
Constructors
Match()
protected Match()
Methods
Create<T>(Func<object, Type, bool>, Expression<Func<T>>)
Initializes the matcher with the condition that will be checked in order to match invocation values.
The condition predicate of this overload will not only be provided with a
method argument, but also with the associated parameter's type. This parameter type essentially
overrides T in cases where the latter is a type matcher. Therefore,
use this method overload if you want your custom matcher to work together with type matchers.
public static T Create<T>(Func<object, Type, bool> condition, Expression<Func<T>> renderExpression)
Parameters
conditionFunc<object, Type, bool>The condition to match against actual values.
This function will be passed the invocation argument, as well as the type of the associated parameter.
renderExpressionExpression<Func<T>>A lambda representation of the matcher.
Returns
- T
Type Parameters
T
Create<T>(Predicate<T>)
Initializes the matcher with the condition that will be checked in order to match invocation values.
public static T Create<T>(Predicate<T> condition)
Parameters
conditionPredicate<T>The condition to match against actual values.
Returns
- T
Type Parameters
T
Create<T>(Predicate<T>, Expression<Func<T>>)
Initializes the matcher with the condition that will be checked in order to match invocation values.
public static T Create<T>(Predicate<T> condition, Expression<Func<T>> renderExpression)
Parameters
conditionPredicate<T>The condition to match against actual values.
renderExpressionExpression<Func<T>>A lambda representation of the matcher, to be used when rendering error messages, such as
() => It.IsAny<string<().
Returns
- T
Type Parameters
T