Class It
- Namespace
- Moq
- Assembly
- Moq.dll
Allows the specification of a matching condition for an argument in a method invocation, rather than a specific argument value. "It" refers to the argument being matched.
public static class It
- Inheritance
-
It
- Inherited Members
Remarks
This class allows the setup to match a method invocation with an arbitrary value, with a value in a specified range, or even one that matches a given predicate.
Methods
IsAny<TValue>()
Matches any value of the given TValue type.
public static TValue IsAny<TValue>()
Returns
- TValue
Type Parameters
TValueType of the value.
Examples
// Throws an exception for a call to Remove with any string value.
mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());
Remarks
Typically used when the actual argument value for a method call is not relevant.
IsInRange<TValue>(TValue, TValue, Range)
Matches any value that is in the range specified.
public static TValue IsInRange<TValue>(TValue from, TValue to, Range rangeKind) where TValue : IComparable
Parameters
fromTValueThe lower bound of the range.
toTValueThe upper bound of the range.
rangeKindRangeThe kind of range. See Range.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
The following example shows how to expect a method call with an integer argument within the 0..100 range.
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsInRange(0, 100, Range.Inclusive)))
.Returns(false);
IsIn<TValue>(IEnumerable<TValue>)
Matches any value that is present in the sequence specified.
public static TValue IsIn<TValue>(IEnumerable<TValue> items)
Parameters
itemsIEnumerable<TValue>The sequence of possible values.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
The following example shows how to expect a method call with an integer argument with value from a list.
var values = new List<int> { 1, 2, 3 };
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsIn(values)))
.Returns(false);
IsIn<TValue>(IEnumerable<TValue>, IEqualityComparer<TValue>)
Matches any value that is present in the sequence specified.
public static TValue IsIn<TValue>(IEnumerable<TValue> items, IEqualityComparer<TValue> comparer)
Parameters
itemsIEnumerable<TValue>The sequence of possible values.
comparerIEqualityComparer<TValue>An IEqualityComparer<T> with which the values should be compared.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
IsIn<TValue>(params TValue[])
Matches any value that is present in the sequence specified.
public static TValue IsIn<TValue>(params TValue[] items)
Parameters
itemsTValue[]The sequence of possible values.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
The following example shows how to expect a method call with an integer argument with a value of 1, 2, or 3.
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsIn(1, 2, 3)))
.Returns(false);
IsNotIn<TValue>(IEnumerable<TValue>)
Matches any value that is not found in the sequence specified.
public static TValue IsNotIn<TValue>(IEnumerable<TValue> items)
Parameters
itemsIEnumerable<TValue>The sequence of disallowed values.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
The following example shows how to expect a method call with an integer argument with value not found from a list.
var values = new List<int> { 1, 2, 3 };
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsNotIn(values)))
.Returns(false);
IsNotIn<TValue>(IEnumerable<TValue>, IEqualityComparer<TValue>)
Matches any value that is not found in the sequence specified.
public static TValue IsNotIn<TValue>(IEnumerable<TValue> items, IEqualityComparer<TValue> comparer)
Parameters
itemsIEnumerable<TValue>The sequence of disallowed values.
comparerIEqualityComparer<TValue>An IEqualityComparer<T> with which the values should be compared.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
IsNotIn<TValue>(params TValue[])
Matches any value that is not found in the sequence specified.
public static TValue IsNotIn<TValue>(params TValue[] items)
Parameters
itemsTValue[]The sequence of disallowed values.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
The following example shows how to expect a method call with an integer argument of any value except 1, 2, or 3.
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsNotIn(1, 2, 3)))
.Returns(false);
IsNotNull<TValue>()
Matches any value of the given TValue type, except null.
public static TValue IsNotNull<TValue>()
Returns
- TValue
Type Parameters
TValueType of the value.
IsRegex(string)
Matches a string argument if it matches the given regular expression pattern.
public static string IsRegex(string regex)
Parameters
regexstringThe pattern to use to match the string argument value.
Returns
Examples
The following example shows how to expect a call to a method where the string argument matches the given regular expression:
mock.Setup(x => x.Check(It.IsRegex("[a-z]+")))
.Returns(1);
IsRegex(string, RegexOptions)
Matches a string argument if it matches the given regular expression pattern.
public static string IsRegex(string regex, RegexOptions options)
Parameters
regexstringThe pattern to use to match the string argument value.
optionsRegexOptionsThe options used to interpret the pattern.
Returns
Examples
The following example shows how to expect a call to a method where the string argument matches the given regular expression, in a case insensitive way:
mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase)))
.Returns(1);
Is<TValue>(Expression<Func<object, Type, bool>>)
Matches any value that satisfies the given predicate.
Use this overload when you specify a type matcher for TValue.
The match callback you provide will then receive the actual parameter type
as well as the invocation argument.
public static TValue Is<TValue>(Expression<Func<object, Type, bool>> match)
Parameters
matchExpression<Func<object, Type, bool>>The predicate used to match the method argument.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Remarks
Allows the specification of a predicate to perform matching of method call arguments.
Is<TValue>(Expression<Func<TValue, bool>>)
Matches any value that satisfies the given predicate.
public static TValue Is<TValue>(Expression<Func<TValue, bool>> match)
Parameters
matchExpression<Func<TValue, bool>>The predicate used to match the method argument.
Returns
- TValue
Type Parameters
TValueType of the argument to check.
Examples
This example shows how to return the value 1 whenever the argument to
the Do method is an even number.
mock.Setup(x => x.Do(It.Is<int>(i => i % 2 == 0)))
.Returns(1);
This example shows how to throw an exception if the argument to the method is a negative number:
mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
.Throws(new ArgumentException());
Remarks
Allows the specification of a predicate to perform matching of method call arguments.
Is<TValue>(TValue, IEqualityComparer<TValue>)
Matches any value that equals the value using the comparer.
To use the default comparer for the specified object, specify the value inline,
i.e.
mock.Verify(service => service.DoWork(value)).
Use this overload when you specify a value and a comparer.
public static TValue Is<TValue>(TValue value, IEqualityComparer<TValue> comparer)
Parameters
valueTValueThe value to match with.
comparerIEqualityComparer<TValue>An IEqualityComparer<T> with which the values should be compared.
Returns
- TValue
Type Parameters
TValueType of the argument to check.