Allows creation custom value matchers that can be used on setups and verification,
completely replacing the built-in It class with your own argument
matching rules.
Namespace:
MoqAssembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)
Syntax
| C# |
|---|
public class Match<T> : Match |
Type Parameters
- T
- Type of the value to match.
Remarks
The argument matching is used to determine whether a concrete
invocation in the mock matches a given setup. This
matching mechanism is fully extensible.
Examples
Creating a custom matcher is straightforward. You just need to create a method
that returns a value from a call to Create(Predicate<(Of <(T>)>)) with
your matching condition and optional friendly render expression:
CopyC#
This method can be used in any mock setup invocation:
CopyC#
At runtime, Moq knows that the return value was a matcher and
evaluates your predicate with the actual value passed into your predicate.
CopyC#
Now we can invoke this static method instead of an argument in an
invocation:
CopyC#
public Order IsBigOrder() { return Match<Order>.Create( o => o.GrandTotal >= 5000, /* a friendly expression to render on failures */ () => IsBigOrder()); }
mock.Setup(m => m.Submit(IsBigOrder()).Throws<UnauthorizedAccessException>();
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<IEnumerable<Order>>.Create(orders => orders.Contains(order)); } }
var order = new Order { ... }; var mock = new Mock<IRepository<Order>>(); mock.Setup(x => x.Save(Orders.Contains(order))) .Throws<ArgumentException>();
Inheritance Hierarchy
Object
Match
Moq..::.Match<(Of <(T>)>)
Match
Moq..::.Match<(Of <(T>)>)