Moq Namespace

Moq
Moq Namespace
Send Feedback
Root namespace of Moq (pronounced "Mock-you" or just "Mock"), the only mocking library for .NET and Silverlight developed from scratch to take full advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions) that make it the most productive, simple and refactoring-friendly mocking library available.

See the online quickstarts for more examples than those available in this code documentation.

The Mock<T> class is the core of the library, so it's a good place to start.

You can also read blog entries from around the world about Moq.

Classes

Class | Description
It | 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.
Match<(Of <(T>)>) | 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.
Mock | Base class for mocks and static helper class with methods that apply to mocked objects, such as Get<(Of <(T>)>)(T) to retrieve a Mock<(Of <(T>)>) from an object instance.
Mock<(Of <(T>)>) | Provides a mock implementation of T.
MockException | Exception thrown by mocks when setups are not matched, the mock is not properly setup, etc.
MockExtensions | Provides additional methods on mocks.
MockFactory | Utility factory class to use to construct multiple mocks when consistent verification is desired for all of them.
Mocks | Allows querying the universe of mocks for those that behave according to the query specification.
QueryableMockExtensions | Helper extensions that are used by the query translator.
Times | Defines the number of invocations allowed by a mocked method.

Enumerations

Enumeration | Description
DefaultValue | Determines the way default values are generated calculated for loose mocks.
MockBehavior | Options to customize the behavior of the mock.
Range | Kind of range to use in a filter specified through IsInRange<(Of <(TValue>)>)(TValue, TValue, Range).

Send comments on this topic to moqdisc@googlegroups.com


DefaultValue Enumeration

DefaultValue Enumeration
See Also Send Feedback
Determines the way default values are generated calculated for loose mocks.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public enum DefaultValue

Members

Member name | Description
Empty | Default behavior, which generates empty values for value types (i.e. default(int)), empty array and enumerables, and nulls for all other reference types.
Mock | Whenever the default value generated by Empty is null, replaces this value with a mock (if the type can be mocked).

See Also


It Class

It Class
Members See Also Send Feedback
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.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class It

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.

Inheritance Hierarchy

Object
Moq..::.It

See Also

It Members


It Members

[image]

Moq
It Members
It Class Methods See Also Send Feedback

The It type exposes the following members.

Methods

Name | Description
Is<(Of <(TValue>)>) | Matches any value that satisfies the given predicate.
IsAny<(Of <(TValue>)>) | Matches any value of the given TValue type.
IsInRange<(Of <(TValue>)>) | Matches any value that is in the range specified.
IsRegex | Overloaded.

See Also

It Class


It Methods

[image]

Moq
It Methods
It Class See Also Send Feedback

The It type exposes the following members.

Methods

Name | Description
Is<(Of <(TValue>)>) | Matches any value that satisfies the given predicate.
IsAny<(Of <(TValue>)>) | Matches any value of the given TValue type.
IsInRange<(Of <(TValue>)>) | Matches any value that is in the range specified.
IsRegex | Overloaded.

See Also

It Class


Is(TValue) Method

It..::.Is<(Of <(TValue>)>) Method
It Class Example See Also Send Feedback
Matches any value that satisfies the given predicate.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static TValue Is<TValue>(
    Expression<Predicate<TValue>> match
)

Parameters

match
Type: Expression<(Of <(Predicate<(Of <(TValue>)>)>)>)
The predicate used to match the method argument.

Type Parameters

TValue
Type of the argument to check.

Remarks

Allows the specification of a predicate to perform matching of method call arguments.

Examples

This example shows how to return the value 1 whenever the argument to the Do method is an even number.
CopyC#
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:
CopyC#
mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
.Throws(new ArgumentException());

See Also

It Class


IsAny(TValue) Method

It..::.IsAny<(Of <(TValue>)>) Method
It Class Example See Also Send Feedback
Matches any value of the given TValue type.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static TValue IsAny<TValue>()

Type Parameters

TValue
Type of the value.

Remarks

Typically used when the actual argument value for a method call is not relevant.

Examples

CopyC#
// Throws an exception for a call to Remove with any string value.
mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());

See Also

It Class


IsInRange(TValue) Method

It..::.IsInRange<(Of <(TValue>)>) Method
It Class Example See Also Send Feedback
Matches any value that is in the range specified.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static TValue IsInRange<TValue>(
    TValue from,
    TValue to,
    Range rangeKind
)
where TValue : IComparable

Parameters

from
Type: TValue
The lower bound of the range.
to
Type: TValue
The upper bound of the range.
rangeKind
Type: Moq..::.Range
The kind of range. See Range.

Type Parameters

TValue
Type 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.
CopyC#
mock.Setup(x => x.HasInventory(
It.IsAny<string>(),
It.IsInRange(0, 100, Range.Inclusive)))
.Returns(false);

See Also

It Class


IsRegex Method

[image]

Moq
It..::.IsRegex Method
It Class See Also Send Feedback

Overload List

Name | Description
IsRegex(String) | Matches a string argument if it matches the given regular expression pattern.
IsRegex(String, RegexOptions) | Matches a string argument if it matches the given regular expression pattern.

See Also

It Class
It Members


IsRegex Method ()

It..::.IsRegex Method ()
It Class Example See Also Send Feedback
Matches a string argument if it matches the given regular expression pattern.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static string IsRegex(
    string regex
)

Parameters

regex
Type: String
The pattern to use to match the string argument value.

Examples

The following example shows how to expect a call to a method where the string argument matches the given regular expression:
CopyC#
mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);

See Also

It Class
IsRegex Overload


IsRegex Method (, )

It..::.IsRegex Method (, )
It Class Example See Also Send Feedback
Matches a string argument if it matches the given regular expression pattern.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static string IsRegex(
    string regex,
    RegexOptions options
)

Parameters

regex
Type: String
The pattern to use to match the string argument value.
options
Type: RegexOptions
The options used to interpret the pattern.

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:
CopyC#
mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);

See Also

It Class
IsRegex Overload


Match(T) Class

Match<(Of <(T>)>) Class
Members Example See Also Send Feedback
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: Moq
Assembly: 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#
public Order IsBigOrder()
{
    return Match<Order>.Create(
        o => o.GrandTotal >= 5000,
        /* a friendly expression to render on failures */
        () => IsBigOrder());
}
 This method can be used in any mock setup invocation:
CopyC#
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:

CopyC#
public static class Orders
{
    public static IEnumerable<Order> Contains(Order order)
    {
        return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
    }
}
 Now we can invoke this static method instead of an argument in an invocation:
CopyC#
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>)>)

See Also

Match<(Of <(T>)>) Members


Match(T) Members

[image]

Moq
Match<(Of <(T>)>) Members
Match<(Of <(T>)>) Class Methods See Also Send Feedback

Methods

Name | Description
Create | Overloaded.

See Also

Match<(Of <(T>)>) Class


Match(T) Methods

[image]

Moq
Match<(Of <(T>)>) Methods
Match<(Of <(T>)>) Class See Also Send Feedback

Methods

Name | Description
Create | Overloaded.

See Also

Match<(Of <(T>)>) Class


Create Method

[image]

Moq
Match<(Of <(T>)>)..::.Create Method
Match<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
Create(Predicate<(Of <(T>)>)) | Initializes the match with the condition that will be checked in order to match invocation values.
Create(Predicate<(Of <(T>)>), Expression<(Of <(Func<(Of <(T>)>)>)>)) |

See Also

Match<(Of <(T>)>) Class
Match<(Of <(T>)>) Members


Create Method ((T))

Match<(Of <(T>)>)..::.Create Method (<(Of <(T>)>))
Match<(Of <(T>)>) Class See Also Send Feedback
Initializes the match with the condition that will be checked in order to match invocation values.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static T Create(
    Predicate<T> condition
)

Parameters

condition
Type: Predicate<(Of <( T >)>)
The condition to match against actual values.

Remarks

See Also

Match<(Of <(T>)>) Class
Create Overload
Moq Namespace
Moq..::.Match<(Of <(T>)>)


Create Method ((T), ((T)))

Match<(Of <(T>)>)..::.Create Method (<(Of <(T>)>), <(Of <(<(Of <(T>)>)>)>))
Match<(Of <(T>)>) Class See Also Send Feedback

[Missing <summary> documentation for M:Moq.Match`1.Create(System.Predicate{`0},System.Linq.Expressions.Expression{System.Func{`0}})]

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static T Create(
    Predicate<T> condition,
    Expression<Func<T>> renderExpression
)

Parameters

condition
Type: Predicate<(Of <( T >)>)

[Missing <param name="condition"/> documentation for "M:Moq.Match`1.Create(System.Predicate{`0},System.Linq.Expressions.Expression{System.Func{`0}})"]

renderExpression
Type: Expression<(Of <(Func<(Of <( T >)>)>)>)

[Missing <param name="renderExpression"/> documentation for "M:Moq.Match`1.Create(System.Predicate{`0},System.Linq.Expressions.Expression{System.Func{`0}})"]

See Also

Match<(Of <(T>)>) Class
Create Overload


Mock Class

Mock Class
Members See Also Send Feedback
Base class for mocks and static helper class with methods that apply to mocked objects, such as Get<(Of <(T>)>)(T) to retrieve a Mock<(Of <(T>)>) from an object instance.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public abstract class Mock

Inheritance Hierarchy

Object
Moq..::.Mock
Moq..::.Mock<(Of <(T>)>)

See Also

Mock Members


Mock Members

[image]

Moq
Mock Members
Mock Class Constructors Methods Properties See Also Send Feedback

The Mock type exposes the following members.

Constructors

Name | Description
Mock | Initializes a new instance of the Mock class.

Methods

Name | Description
As<(Of <(TInterface>)>) | Adds an interface implementation to the mock, allowing setups to be specified for it.
Get<(Of <(T>)>) | Retrieves the mock object for the given object instance.
OnGetObject | Returns the mocked object value.
Verify | Verifies that all verifiable expectations have been met.
VerifyAll | Verifies all expectations regardless of whether they have been flagged as verifiable.

Properties

Name | Description
Behavior | Behavior of the mock, according to the value set in the constructor.
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.
Object | Gets the mocked object instance, which is of the mocked type T.

See Also

Mock Class


Mock Constructor

Mock Constructor
Mock Class See Also Send Feedback
Initializes a new instance of the Mock class.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
protected Mock()

See Also

Mock Class


Mock Methods

[image]

Moq
Mock Methods
Mock Class See Also Send Feedback

The Mock type exposes the following members.

Methods

Name | Description
As<(Of <(TInterface>)>) | Adds an interface implementation to the mock, allowing setups to be specified for it.
Get<(Of <(T>)>) | Retrieves the mock object for the given object instance.
OnGetObject | Returns the mocked object value.
Verify | Verifies that all verifiable expectations have been met.
VerifyAll | Verifies all expectations regardless of whether they have been flagged as verifiable.

See Also

Mock Class


As(TInterface) Method

Mock..::.As<(Of <(TInterface>)>) Method
Mock Class Example See Also Send Feedback
Adds an interface implementation to the mock, allowing setups to be specified for it.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual Mock<TInterface> As<TInterface>()
where TInterface : class

Type Parameters

TInterface
Type 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.

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:
CopyC#
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();

Exceptions

Exception | Condition
InvalidOperationException | The mock type has already been generated by accessing the Object property.
ArgumentException | The TInterface specified is not an interface.

See Also

Mock Class


Get(T) Method

Mock..::.Get<(Of <(T>)>) Method
Mock Class Example See Also Send Feedback
Retrieves the mock object for the given object instance.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Mock<T> Get<T>(
    T mocked
)
where T : class

Parameters

mocked
Type: T
The instance of the mocked object.

Type Parameters

T
Type of the mock to retrieve. Can be omitted as it's inferred from the object instance passed in as the mocked instance.

Return Value

The mock associated with the mocked object.

Examples

The following example shows how to add a new setup to an object instance which is not the original Mock<(Of <(T>)>) but rather the object associated with it:
CopyC#
// 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);
mock.Setup(req => req.AppRelativeCurrentExecutionFilePath)
     .Returns(tempUrl);

Exceptions

Exception | Condition
ArgumentException | The received mocked instance was not created by Moq.

See Also

Mock Class


OnGetObject Method

Mock..::.OnGetObject Method
Mock Class See Also Send Feedback
Returns the mocked object value.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
protected abstract Object OnGetObject()

See Also

Mock Class


Verify Method

Mock..::.Verify Method
Mock Class Example See Also Send Feedback
Verifies that all verifiable expectations have been met.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
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:
CopyC#
var mock = new Mock<IWarehouse>();
this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
...
// other test code
...
// Will throw if the test code has didn't call HasInventory.
this.Verify();

Exceptions

Exception | Condition
Moq..::.MockException | Not all verifiable expectations were met.

See Also

Mock Class


VerifyAll Method

Mock..::.VerifyAll Method
Mock Class Example See Also Send Feedback
Verifies all expectations regardless of whether they have been flagged as verifiable.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifyAll()

Examples

This example sets up an expectation without marking it as verifiable. After the mock is used, a VerifyAll()()() call is issued on the mock to ensure that all expectations are met:
CopyC#
var mock = new Mock<IWarehouse>();
this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
...
// other test code
...
// Will throw if the test code has didn't call HasInventory, even
// that expectation was not marked as verifiable.
this.VerifyAll();

Exceptions

Exception | Condition
Moq..::.MockException | At least one expectation was not met.

See Also

Mock Class


Mock Properties

[image]

Moq
Mock Properties
Mock Class See Also Send Feedback

The Mock type exposes the following members.

Properties

Name | Description
Behavior | Behavior of the mock, according to the value set in the constructor.
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.
Object | Gets the mocked object instance, which is of the mocked type T.

See Also

Mock Class


Behavior Property

Mock..::.Behavior Property
Mock Class See Also Send Feedback
Behavior of the mock, according to the value set in the constructor.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual MockBehavior Behavior { get; internal set; }

See Also

Mock Class


CallBase Property

Mock..::.CallBase Property
Mock Class See Also Send Feedback
Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual bool CallBase { get; set; }

See Also

Mock Class


DefaultValue Property

Mock..::.DefaultValue Property
Mock Class See Also Send Feedback
Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual DefaultValue DefaultValue { get; set; }

See Also

Mock Class


Object Property

Mock..::.Object Property
Mock Class See Also Send Feedback
Gets the mocked object instance, which is of the mocked type T.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Object Object { get; }

See Also

Mock Class


Mock(T) Class

Mock<(Of <(T>)>) Class
Members Example See Also Send Feedback
Provides a mock implementation of T.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public class Mock<T> : Mock
where T : class

Type Parameters

T
Type to mock, which can be an interface or a class.

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<(Of <(T>)>)(MockBehavior) constructor.

Examples

The following example shows establishing setups with specific values for method invocations:
CopyC#
// Arrange
var order = new Order(TALISKER, 50);
var mock = new Mock<IWarehouse>();

mock.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);

// Act
order.Fill(mock.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:
CopyC#
// Arrange
var order = new Order(TALISKER, 50);
var mock = new Mock<IWarehouse>();

// shows how to expect a value within a range
mock.Setup(x => x.HasInventory(
            It.IsAny<string>(),
            It.IsInRange(0, 100, Range.Inclusive)))
     .Returns(false);

// shows how to throw for unexpected calls.
mock.Setup(x => x.Remove(
            It.IsAny<string>(),
            It.IsAny<int>()))
     .Throws(new InvalidOperationException());

// Act
order.Fill(mock.Object);

// Assert
Assert.False(order.IsFilled);

Inheritance Hierarchy

Object
Moq..::.Mock
Moq..::.Mock<(Of <(T>)>)

See Also

Mock<(Of <(T>)>) Members


Mock(T) Members

[image]

Moq
Mock<(Of <(T>)>) Members
Mock<(Of <(T>)>) Class Constructors Methods Properties See Also Send Feedback

The Mock<(Of <(T>)>) type exposes the following members.

Constructors

Name | Description
Mock<(Of <(T>)>) | Overloaded.

Methods

Name | Description
As<(Of <(TInterface>)>) | Adds an interface implementation to the mock, allowing setups to be specified for it. (Inherited from Mock.)
OnGetObject | Returns the mocked object value. (Overrides Mock..::.OnGetObject()()().)
Raise | Overloaded.
Setup | Overloaded.
SetupAllProperties | Specifies that the all properties on the mock 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"). The default value for each property will be the one generated as specified by the DefaultValue property for the mock.
SetupGet<(Of <(TProperty>)>) | Specifies a setup on the mocked type for a call to to a property getter.
SetupProperty | Overloaded.
SetupSet | Overloaded.
Verify | Overloaded.
VerifyAll | Verifies all expectations regardless of whether they have been flagged as verifiable. (Inherited from Mock.)
VerifyGet | Overloaded.
VerifySet | Overloaded.

Properties

Name | Description
Behavior | Behavior of the mock, according to the value set in the constructor. (Inherited from Mock.)
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic). (Inherited from Mock.)
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks. (Inherited from Mock.)
Object | Exposes the mocked object instance.

See Also

Mock<(Of <(T>)>) Class


Mock(T) Constructor

[image]

Moq
Mock<(Of <(T>)>) Constructor
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
Mock<(Of <(T>)>)()()() | Initializes an instance of the mock with default behavior.
Mock<(Of <(T>)>)(MockBehavior) | Initializes an instance of the mock with the specified behavior.
Mock<(Of <(T>)>)(array<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)
Mock<(Of <(T>)>)(MockBehavior, array<Object>[]()[]) | Initializes an instance of the mock with a specific behavior with the given constructor arguments for the class.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


Mock(T) Constructor

Mock<(Of <(T>)>) Constructor
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Initializes an instance of the mock with default behavior.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock()

Examples

CopyC#
var mock = new Mock<IFormatProvider>();

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Overload


Mock(T) Constructor (MockBehavior)

Mock<(Of <(T>)>) Constructor (MockBehavior)
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Initializes an instance of the mock with the specified behavior.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock(
    MockBehavior behavior
)

Parameters

behavior
Type: Moq..::.MockBehavior
Behavior of the mock.

Examples

CopyC#
var mock = new Mock<IFormatProvider>(MockBehavior.Relaxed);

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Overload


Mock(T) Constructor ([])

Mock<(Of <(T>)>) Constructor (array<>[]()[])
Mock<(Of <(T>)>) Class Example See Also Send Feedback
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)

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock(
    params Object[] args
)

Parameters

args
Type: array< Object >[]()[]
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 for classes, not interfaces.

Examples

CopyC#
var mock = new Mock<MyProvider>(someArgument, 25);

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Overload


Mock(T) Constructor (MockBehavior, [])

Mock<(Of <(T>)>) Constructor (MockBehavior, array<>[]()[])
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Initializes an instance of the mock with a specific behavior with the given constructor arguments for the class.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock(
    MockBehavior behavior,
    params Object[] args
)

Parameters

behavior
Type: Moq..::.MockBehavior
Behavior of the mock.
args
Type: array< Object >[]()[]
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.

Examples

CopyC#
var mock = new Mock<MyProvider>(someArgument, 25);

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Overload


Mock(T) Methods

[image]

Moq
Mock<(Of <(T>)>) Methods
Mock<(Of <(T>)>) Class See Also Send Feedback

The Mock<(Of <(T>)>) type exposes the following members.

Methods

Name | Description
As<(Of <(TInterface>)>) | Adds an interface implementation to the mock, allowing setups to be specified for it. (Inherited from Mock.)
OnGetObject | Returns the mocked object value. (Overrides Mock..::.OnGetObject()()().)
Raise | Overloaded.
Setup | Overloaded.
SetupAllProperties | Specifies that the all properties on the mock 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"). The default value for each property will be the one generated as specified by the DefaultValue property for the mock.
SetupGet<(Of <(TProperty>)>) | Specifies a setup on the mocked type for a call to to a property getter.
SetupProperty | Overloaded.
SetupSet | Overloaded.
Verify | Overloaded.
VerifyAll | Verifies all expectations regardless of whether they have been flagged as verifiable. (Inherited from Mock.)
VerifyGet | Overloaded.
VerifySet | Overloaded.

See Also

Mock<(Of <(T>)>) Class


OnGetObject Method

Mock<(Of <(T>)>)..::.OnGetObject Method
Mock<(Of <(T>)>) Class See Also Send Feedback
Returns the mocked object value.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
protected override Object OnGetObject()

See Also

Mock<(Of <(T>)>) Class


Raise Method

[image]

Moq
Mock<(Of <(T>)>)..::.Raise Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
Raise(Action<(Of <(T>)>), EventArgs) | Raises the event referenced in eventExpression using the given sender and args arguments.
Raise(Action<(Of <(T>)>), array<Object>[]()[]) | Raises the event referenced in eventExpression using the given sender and args arguments for a non-EventHandler typed event.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


Raise Method ((T), )

Mock<(Of <(T>)>)..::.Raise Method (<(Of <(T>)>), )
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Raises the event referenced in eventExpression using the given sender and args arguments.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Raise(
    Action<T> eventExpression,
    EventArgs args
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)

[Missing <param name="eventExpression"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.EventArgs)"]

args
Type: EventArgs

[Missing <param name="args"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.EventArgs)"]

Examples

The following example shows how to raise a PropertyChanged()()() event:
CopyC#
var mock = new Mock<IViewModel>();

mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));

Examples

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:
CopyC#
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

Exception | Condition
ArgumentException | The args argument is invalid for the target event invocation, or the eventExpression is not an event attach or detach expression.

See Also

Mock<(Of <(T>)>) Class
Raise Overload


Raise Method ((T), [])

Mock<(Of <(T>)>)..::.Raise Method (<(Of <(T>)>), array<>[]()[])
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Raises the event referenced in eventExpression using the given sender and args arguments for a non-EventHandler typed event.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Raise(
    Action<T> eventExpression,
    params Object[] args
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)

[Missing <param name="eventExpression"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.Object[])"]

args
Type: array< Object >[]()[]

[Missing <param name="args"/> documentation for "M:Moq.Mock`1.Raise(System.Action{`0},System.Object[])"]

Examples

The following example shows how to raise a custom event that does not adhere to the standard EventHandler:
CopyC#
var mock = new Mock<IViewModel>();

mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);

Exceptions

Exception | Condition
ArgumentException | The args arguments are invalid for the target event invocation, or the eventExpression is not an event attach or detach expression.

See Also

Mock<(Of <(T>)>) Class
Raise Overload


Setup Method

[image]

Moq
Mock<(Of <(T>)>)..::.Setup Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
Setup(Expression<(Of <(Action<(Of <(T>)>)>)>)) | Specifies a setup on the mocked type for a call to to a void method.
Setup<(Of <(TResult>)>)(Expression<(Of <(Func<(Of <(T, TResult>)>)>)>)) | Specifies a setup on the mocked type for a call to to a value returning method.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


Setup Method (((T)))

Mock<(Of <(T>)>)..::.Setup Method (<(Of <(<(Of <(T>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a void method.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public ISetup<T> Setup(
    Expression<Action<T>> expression
)

Parameters

expression
Type: Expression<(Of <(Action<(Of <( T >)>)>)>)
Lambda expression that specifies the expected method invocation.

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.

Examples

CopyC#
var mock = new Mock<IProcessor>();
mock.Setup(x => x.Execute("ping"));

See Also

Mock<(Of <(T>)>) Class
Setup Overload


Setup(TResult) Method (((T, TResult)))

Mock<(Of <(T>)>)..::.Setup<(Of <(TResult>)>) Method (<(Of <(<(Of <(T, TResult>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a value returning method.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public ISetup<T, TResult> Setup<TResult>(
    Expression<Func<T, TResult>> expression
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TResult>)>)>)>)
Lambda expression that specifies the method invocation.

Type Parameters

TResult
Type of the return value. Typically omitted as it can be inferred from the expression.

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.

Examples

CopyC#
mock.Setup(x => x.HasInventory("Talisker", 50)).Returns(true);

See Also

Mock<(Of <(T>)>) Class
Setup Overload


SetupAllProperties Method

Mock<(Of <(T>)>)..::.SetupAllProperties Method
Mock<(Of <(T>)>) Class See Also Send Feedback
Specifies that the all properties on the mock 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"). The default value for each property will be the one generated as specified by the DefaultValue property for the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void SetupAllProperties()

Remarks

If the mock DefaultValue is set to Mock, the mocked default values will also get all properties setup recursively.

See Also

Mock<(Of <(T>)>) Class


SetupGet(TProperty) Method

Mock<(Of <(T>)>)..::.SetupGet<(Of <(TProperty>)>) Method
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a property getter.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public ISetupGetter<T, TProperty> SetupGet<TProperty>(
    Expression<Func<T, TProperty>> expression
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Lambda expression that specifies the property getter.

Type Parameters

TProperty
Type of the property. Typically omitted as it can be inferred from the expression.

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.

Examples

CopyC#
mock.SetupGet(x => x.Suspended)
     .Returns(true);

See Also

Mock<(Of <(T>)>) Class


SetupProperty Method

[image]

Moq
Mock<(Of <(T>)>)..::.SetupProperty Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
SetupProperty<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(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").
SetupProperty<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(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").

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


SetupProperty(TProperty) Method (((T, TProperty)))

Mock<(Of <(T>)>)..::.SetupProperty<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
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").

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void SetupProperty<TProperty>(
    Expression<Func<T, TProperty>> property
)

Parameters

property
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Property expression to stub.

Type Parameters

TProperty
Type 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:
CopyC#
var mock = new Mock<IHaveValue>();
mock.Stub(v => v.Value);
 After the Stub call has been issued, setting and retrieving the object value will behave as expected:
CopyC#
IHaveValue v = mock.Object;

v.Value = 5;
Assert.Equal(5, v.Value);

See Also

Mock<(Of <(T>)>) Class
SetupProperty Overload


SetupProperty(TProperty) Method (((T, TProperty)), TProperty)

Mock<(Of <(T>)>)..::.SetupProperty<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>), TProperty)
Mock<(Of <(T>)>) Class Example See Also Send Feedback
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").

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void SetupProperty<TProperty>(
    Expression<Func<T, TProperty>> property,
    TProperty initialValue
)

Parameters

property
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Property expression to stub.
initialValue
Type: TProperty
Initial value for the property.

Type Parameters

TProperty
Type 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:
CopyC#
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:
CopyC#
IHaveValue v = mock.Object;
// Initial value was stored
Assert.Equal(5, v.Value);

// New value set which changes the initial value
v.Value = 6;
Assert.Equal(6, v.Value);

See Also

Mock<(Of <(T>)>) Class
SetupProperty Overload


SetupSet Method

[image]

Moq
Mock<(Of <(T>)>)..::.SetupSet Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
SetupSet(Action<(Of <(T>)>)) | Specifies a setup on the mocked type for a call to to a property setter.
SetupSet<(Of <(TProperty>)>)(Action<(Of <(T>)>)) | Specifies a setup on the mocked type for a call to to a property setter.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


SetupSet Method ((T))

Mock<(Of <(T>)>)..::.SetupSet Method (<(Of <(T>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a property setter.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public ISetup<T> SetupSet(
    Action<T> setterExpression
)

Parameters

setterExpression
Type: Action<(Of <( T >)>)
Lambda expression that sets a property to a value.

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.

Examples

CopyC#
mock.SetupSet(x => x.Suspended = true);

See Also

Mock<(Of <(T>)>) Class
SetupSet Overload


SetupSet(TProperty) Method ((T))

Mock<(Of <(T>)>)..::.SetupSet<(Of <(TProperty>)>) Method (<(Of <(T>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a property setter.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public ISetupSetter<T, TProperty> SetupSet<TProperty>(
    Action<T> setterExpression
)

Parameters

setterExpression
Type: Action<(Of <( T >)>)
Lambda expression that sets a property to a value.

Type Parameters

TProperty
Type of the property. Typically omitted as it can be inferred from the expression.

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.

Examples

CopyC#
mock.SetupSet(x => x.Suspended = true);

See Also

Mock<(Of <(T>)>) Class
SetupSet Overload


Verify Method

[image]

Moq
Mock<(Of <(T>)>)..::.Verify Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
Verify()()() | Verifies that all verifiable expectations have been met. (Inherited from Mock.)
Verify(Expression<(Of <(Action<(Of <(T>)>)>)>)) | Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.
Verify<(Of <(TResult>)>)(Expression<(Of <(Func<(Of <(T, TResult>)>)>)>)) | Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.
Verify(Expression<(Of <(Action<(Of <(T>)>)>)>), Times) | Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.
Verify(Expression<(Of <(Action<(Of <(T>)>)>)>), String) | Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjuntion with the default Loose.
Verify<(Of <(TResult>)>)(Expression<(Of <(Func<(Of <(T, TResult>)>)>)>), Times) | Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.
Verify<(Of <(TResult>)>)(Expression<(Of <(Func<(Of <(T, TResult>)>)>)>), String) | Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.
Verify(Expression<(Of <(Action<(Of <(T>)>)>)>), Times, String) | Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjuntion with the default Loose.
Verify<(Of <(TResult>)>)(Expression<(Of <(Func<(Of <(T, TResult>)>)>)>), Times, String) | Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


Verify Method (((T)))

Mock<(Of <(T>)>)..::.Verify Method (<(Of <(<(Of <(T>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify(
    Expression<Action<T>> expression
)

Parameters

expression
Type: Expression<(Of <(Action<(Of <( 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify(TResult) Method (((T, TResult)))

Mock<(Of <(T>)>)..::.Verify<(Of <(TResult>)>) Method (<(Of <(<(Of <(T, TResult>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify<TResult>(
    Expression<Func<T, TResult>> expression
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TResult>)>)>)>)
Expression to verify.

Type Parameters

TResult
Type 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify Method (((T)), Times)

Mock<(Of <(T>)>)..::.Verify Method (<(Of <(<(Of <(T>)>)>)>), Times)
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify(
    Expression<Action<T>> expression,
    Times times
)

Parameters

expression
Type: Expression<(Of <(Action<(Of <( T >)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify Method (((T)), )

Mock<(Of <(T>)>)..::.Verify Method (<(Of <(<(Of <(T>)>)>)>), )
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify(
    Expression<Action<T>> expression,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Action<(Of <( T >)>)>)>)
Expression to verify.
failMessage
Type: String
Message to show if verification fails.

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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify(TResult) Method (((T, TResult)), Times)

Mock<(Of <(T>)>)..::.Verify<(Of <(TResult>)>) Method (<(Of <(<(Of <(T, TResult>)>)>)>), Times)
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify<TResult>(
    Expression<Func<T, TResult>> expression,
    Times times
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TResult>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.

Type Parameters

TResult
Type of return value from the expression.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify(TResult) Method (((T, TResult)), )

Mock<(Of <(T>)>)..::.Verify<(Of <(TResult>)>) Method (<(Of <(<(Of <(T, TResult>)>)>)>), )
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify<TResult>(
    Expression<Func<T, TResult>> expression,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TResult>)>)>)>)
Expression to verify.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

TResult
Type 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify Method (((T)), Times, )

Mock<(Of <(T>)>)..::.Verify Method (<(Of <(<(Of <(T>)>)>)>), Times, )
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message. Use in conjuntion with the default Loose.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify(
    Expression<Action<T>> expression,
    Times times,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Action<(Of <( T >)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.
failMessage
Type: String
Message to show if verification fails.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


Verify(TResult) Method (((T, TResult)), Times, )

Mock<(Of <(T>)>)..::.Verify<(Of <(TResult>)>) Method (<(Of <(<(Of <(T, TResult>)>)>)>), Times, )
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a specific invocation matching the given expression was performed on the mock, specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void Verify<TResult>(
    Expression<Func<T, TResult>> expression,
    Times times,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TResult>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

TResult
Type of return value from the expression.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
Verify Overload


VerifyGet Method

[image]

Moq
Mock<(Of <(T>)>)..::.VerifyGet Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
VerifyGet<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)) | Verifies that a property was read on the mock.
VerifyGet<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), Times) | Verifies that a property was read on the mock.
VerifyGet<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), String) | Verifies that a property was read on the mock, specifying a failure error message.
VerifyGet<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), Times, String) | Verifies that a property was read on the mock, specifying a failure error message.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


VerifyGet(TProperty) Method (((T, TProperty)))

Mock<(Of <(T>)>)..::.VerifyGet<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a property was read on the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifyGet<TProperty>(
    Expression<Func<T, TProperty>> expression
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Expression to verify.

Type Parameters

TProperty
Type 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
VerifyGet Overload


VerifyGet(TProperty) Method (((T, TProperty)), Times)

Mock<(Of <(T>)>)..::.VerifyGet<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>), Times)
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a property was read on the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifyGet<TProperty>(
    Expression<Func<T, TProperty>> expression,
    Times times
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.

Type Parameters

TProperty
Type of the property to verify. Typically omitted as it can be inferred from the expression's return type.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
VerifyGet Overload


VerifyGet(TProperty) Method (((T, TProperty)), )

Mock<(Of <(T>)>)..::.VerifyGet<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>), )
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a property was read on the mock, specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifyGet<TProperty>(
    Expression<Func<T, TProperty>> expression,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Expression to verify.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

TProperty
Type 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
VerifyGet Overload


VerifyGet(TProperty) Method (((T, TProperty)), Times, )

Mock<(Of <(T>)>)..::.VerifyGet<(Of <(TProperty>)>) Method (<(Of <(<(Of <(T, TProperty>)>)>)>), Times, )
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a property was read on the mock, specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifyGet<TProperty>(
    Expression<Func<T, TProperty>> expression,
    Times times,
    string failMessage
)

Parameters

expression
Type: Expression<(Of <(Func<(Of <( T, TProperty>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

TProperty
Type of the property to verify. Typically omitted as it can be inferred from the expression's return type.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
VerifyGet Overload


VerifySet Method

[image]

Moq
Mock<(Of <(T>)>)..::.VerifySet Method
Mock<(Of <(T>)>) Class See Also Send Feedback

Overload List

Name | Description
VerifySet(Action<(Of <(T>)>)) | Verifies that a property was set on the mock.
VerifySet(Action<(Of <(T>)>), Times) | Verifies that a property was set on the mock.
VerifySet(Action<(Of <(T>)>), String) | Verifies that a property was set on the mock, specifying a failure message.
VerifySet(Action<(Of <(T>)>), Times, String) | Verifies that a property was set on the mock, specifying a failure message.

See Also

Mock<(Of <(T>)>) Class
Mock<(Of <(T>)>) Members


VerifySet Method ((T))

Mock<(Of <(T>)>)..::.VerifySet Method (<(Of <(T>)>))
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a property was set on the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifySet(
    Action<T> setterExpression
)

Parameters

setterExpression
Type: Action<(Of <( 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
VerifySet Overload


VerifySet Method ((T), Times)

Mock<(Of <(T>)>)..::.VerifySet Method (<(Of <(T>)>), Times)
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a property was set on the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifySet(
    Action<T> setterExpression,
    Times times
)

Parameters

setterExpression
Type: Action<(Of <( T >)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
VerifySet Overload


VerifySet Method ((T), )

Mock<(Of <(T>)>)..::.VerifySet Method (<(Of <(T>)>), )
Mock<(Of <(T>)>) Class Example See Also Send Feedback
Verifies that a property was set on the mock, specifying a failure message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifySet(
    Action<T> setterExpression,
    string failMessage
)

Parameters

setterExpression
Type: Action<(Of <( T >)>)
Expression to verify.
failMessage
Type: String
Message 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:
CopyC#
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

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

Mock<(Of <(T>)>) Class
VerifySet Overload


VerifySet Method ((T), Times, )

Mock<(Of <(T>)>)..::.VerifySet Method (<(Of <(T>)>), Times, )
Mock<(Of <(T>)>) Class See Also Send Feedback
Verifies that a property was set on the mock, specifying a failure message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public void VerifySet(
    Action<T> setterExpression,
    Times times,
    string failMessage
)

Parameters

setterExpression
Type: Action<(Of <( T >)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.
failMessage
Type: String
Message to show if verification fails.

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

Mock<(Of <(T>)>) Class
VerifySet Overload


Mock(T) Properties

[image]

Moq
Mock<(Of <(T>)>) Properties
Mock<(Of <(T>)>) Class See Also Send Feedback

The Mock<(Of <(T>)>) type exposes the following members.

Properties

Name | Description
Behavior | Behavior of the mock, according to the value set in the constructor. (Inherited from Mock.)
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic). (Inherited from Mock.)
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks. (Inherited from Mock.)
Object | Exposes the mocked object instance.

See Also

Mock<(Of <(T>)>) Class


Object Property

Mock<(Of <(T>)>)..::.Object Property
Mock<(Of <(T>)>) Class See Also Send Feedback
Exposes the mocked object instance.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual T Object { get; }

See Also

Mock<(Of <(T>)>) Class


MockBehavior Enumeration

MockBehavior Enumeration
See Also Send Feedback
Options to customize the behavior of the mock.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public enum MockBehavior

Members

Member name | Description
Strict | Causes the mock to always throw an exception for invocations that don't have a corresponding setup.
Loose | Will never throw exceptions, returning default values when necessary (null for reference types, zero for value types or empty enumerables and arrays).
Default | Default mock behavior, which equals Loose.

See Also


MockException Class

MockException Class
Members See Also Send Feedback
Exception thrown by mocks when setups are not matched, the mock is not properly setup, etc.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
[SerializableAttribute]
public class MockException : Exception

Remarks

A distinct exception type is provided so that exceptions thrown by the mock can be differentiated in tests that expect other exceptions to be thrown (i.e. ArgumentException).

Richer exception hierarchy/types are not provided as tests typically should not catch or expect exceptions from the mocks. These are typically the result of changes in the tested class or its collaborators implementation, and result in fixes in the mock setup so that they dissapear and allow the test to pass.

Inheritance Hierarchy

Object
Exception
Moq..::.MockException

See Also

MockException Members


MockException Members

[image]

Moq
MockException Members
MockException Class Constructors Methods See Also Send Feedback

The MockException type exposes the following members.

Constructors

Name | Description
MockException | Supports the serialization infrastructure.

Methods

Name | Description
GetObjectData | Supports the serialization infrastructure. (Overrides ExceptionGetObjectData(SerializationInfo, StreamingContext).)

See Also

MockException Class


MockException Constructor

MockException Constructor
MockException Class See Also Send Feedback
Supports the serialization infrastructure.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
protected MockException(
    SerializationInfo info,
    StreamingContext context
)

Parameters

info
Type: SerializationInfo
Serialization information.
context
Type: StreamingContext
Streaming context.

See Also

MockException Class


MockException Methods

[image]

Moq
MockException Methods
MockException Class See Also Send Feedback

The MockException type exposes the following members.

Methods

Name | Description
GetObjectData | Supports the serialization infrastructure. (Overrides ExceptionGetObjectData(SerializationInfo, StreamingContext).)

See Also

MockException Class


GetObjectData Method

MockException..::.GetObjectData Method
MockException Class See Also Send Feedback
Supports the serialization infrastructure.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public override void GetObjectData(
    SerializationInfo info,
    StreamingContext context
)

Parameters

info
Type: SerializationInfo
Serialization information.
context
Type: StreamingContext
Streaming context.

Implements

ISerializableGetObjectData(SerializationInfo, StreamingContext)
_ExceptionGetObjectData(SerializationInfo, StreamingContext)

See Also

MockException Class


MockExtensions Class

MockExtensions Class
Members See Also Send Feedback
Provides additional methods on mocks.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class MockExtensions

Inheritance Hierarchy

Object
Moq..::.MockExtensions

See Also

MockExtensions Members


MockExtensions Members

[image]

Moq
MockExtensions Members
MockExtensions Class Methods See Also Send Feedback

The MockExtensions type exposes the following members.

Methods

Name | Description
SetupSet<(Of <(T, TProperty>)>) | Specifies a setup on the mocked type for a call to to a property setter, regardless of its value.
VerifySet | Overloaded.

See Also

MockExtensions Class


MockExtensions Methods

[image]

Moq
MockExtensions Methods
MockExtensions Class See Also Send Feedback

The MockExtensions type exposes the following members.

Methods

Name | Description
SetupSet<(Of <(T, TProperty>)>) | Specifies a setup on the mocked type for a call to to a property setter, regardless of its value.
VerifySet | Overloaded.

See Also

MockExtensions Class


SetupSet(T, TProperty) Method

MockExtensions..::.SetupSet<(Of <(T, TProperty>)>) Method
MockExtensions Class Example See Also Send Feedback
Specifies a setup on the mocked type for a call to to a property setter, regardless of its value.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static ISetupSetter<T, TProperty> SetupSet<T, TProperty>(
    Mock<T> mock,
    Expression<Func<T, TProperty>> expression
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The target mock for the setup.
expression
Type: Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)
Lambda expression that specifies the property setter.

Type Parameters

T
Type of the mock.
TProperty
Type of the property. Typically omitted as it can be inferred from the expression.

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.

Examples

CopyC#
mock.SetupSet(x => x.Suspended);

See Also

MockExtensions Class


VerifySet Method

[image]

Moq
MockExtensions..::.VerifySet Method
MockExtensions Class See Also Send Feedback

Overload List

Name | Description
VerifySet<(Of <(T, TProperty>)>)(Mock<(Of <(T>)>), Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)) | Verifies that a property has been set on the mock, regarless of its value.
VerifySet<(Of <(T, TProperty>)>)(Mock<(Of <(T>)>), Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), Times) | Verifies that a property has been set on the mock, regardless of the value but only the specified number of times.
VerifySet<(Of <(T, TProperty>)>)(Mock<(Of <(T>)>), Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), String) | Verifies that a property has been set on the mock, specifying a failure error message.
VerifySet<(Of <(T, TProperty>)>)(Mock<(Of <(T>)>), Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>), Times, String) | Verifies that a property has been set on the mock, regardless of the value but only the specified number of times, and specifying a failure error message.

See Also

MockExtensions Class
MockExtensions Members


VerifySet(T, TProperty) Method (Mock(T), ((T, TProperty)))

MockExtensions..::.VerifySet<(Of <(T, TProperty>)>) Method (Mock<(Of <(T>)>), <(Of <(<(Of <(T, TProperty>)>)>)>))
MockExtensions Class Example See Also Send Feedback
Verifies that a property has been set on the mock, regarless of its value.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static void VerifySet<T, TProperty>(
    Mock<T> mock,
    Expression<Func<T, TProperty>> expression
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The mock instance.
expression
Type: Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)
Expression to verify.

Type Parameters

T
Mocked type.
TProperty
Type 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 invocation with specific parameters was performed:
CopyC#
var mock = new Mock<IWarehouse>();
// exercise mock
//...
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed);

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

MockExtensions Class
VerifySet Overload


VerifySet(T, TProperty) Method (Mock(T), ((T, TProperty)), Times)

MockExtensions..::.VerifySet<(Of <(T, TProperty>)>) Method (Mock<(Of <(T>)>), <(Of <(<(Of <(T, TProperty>)>)>)>), Times)
MockExtensions Class Example See Also Send Feedback
Verifies that a property has been set on the mock, regardless of the value but only the specified number of times.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static void VerifySet<T, TProperty>(
    Mock<T> mock,
    Expression<Func<T, TProperty>> expression,
    Times times
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The mock instance.
expression
Type: Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.

Type Parameters

T
Mocked type.
TProperty
Type 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 invocation with specific parameters was performed:
CopyC#
var mock = new Mock<IWarehouse>();
// exercise mock
//...
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed);

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

MockExtensions Class
VerifySet Overload


VerifySet(T, TProperty) Method (Mock(T), ((T, TProperty)), )

MockExtensions..::.VerifySet<(Of <(T, TProperty>)>) Method (Mock<(Of <(T>)>), <(Of <(<(Of <(T, TProperty>)>)>)>), )
MockExtensions Class Example See Also Send Feedback
Verifies that a property has been set on the mock, specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static void VerifySet<T, TProperty>(
    Mock<T> mock,
    Expression<Func<T, TProperty>> expression,
    string failMessage
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The mock instance.
expression
Type: Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)
Expression to verify.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

T
Mocked type.
TProperty
Type 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 invocation with specific parameters was performed:
CopyC#
var mock = new Mock<IWarehouse>();
// exercise mock
//...
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed);

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.

See Also

MockExtensions Class
VerifySet Overload


VerifySet(T, TProperty) Method (Mock(T), ((T, TProperty)), Times, )

MockExtensions..::.VerifySet<(Of <(T, TProperty>)>) Method (Mock<(Of <(T>)>), <(Of <(<(Of <(T, TProperty>)>)>)>), Times, )
MockExtensions Class Example See Also Send Feedback
Verifies that a property has been set on the mock, regardless of the value but only the specified number of times, and specifying a failure error message.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static void VerifySet<T, TProperty>(
    Mock<T> mock,
    Expression<Func<T, TProperty>> expression,
    Times times,
    string failMessage
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The mock instance.
expression
Type: Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>)
Expression to verify.
times
Type: Moq..::.Times
The number of times a method is allowed to be called.
failMessage
Type: String
Message to show if verification fails.

Type Parameters

T
Mocked type.
TProperty
Type 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 invocation with specific parameters was performed:
CopyC#
var mock = new Mock<IWarehouse>();
// exercise mock
//...
// Will throw if the test code didn't set the IsClosed property.
mock.VerifySet(warehouse => warehouse.IsClosed);

Exceptions

Exception | Condition
Moq..::.MockException | The invocation was not performed on the mock.
Moq..::.MockException | The invocation was not call the times specified by times.

See Also

MockExtensions Class
VerifySet Overload


MockFactory Class

MockFactory Class
Members Example See Also Send Feedback
Utility factory class to use to construct multiple mocks when consistent verification is desired for all of them.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public class MockFactory

Remarks

If multiple mocks will be created during a test, passing the desired MockBehavior (if different than the Default or the one passed to the factory constructor) and later verifying each mock can become repetitive and tedious.

This factory class helps in that scenario by providing a simplified creation of multiple mocks with a default MockBehavior (unless overriden by calling Create<(Of <(T>)>)(MockBehavior)) and posterior verification.

Examples

The following is a straightforward example on how to create and automatically verify strict mocks using a MockFactory:
CopyC#
var factory = new MockFactory(MockBehavior.Strict);

var foo = factory.Create<IFoo>();
var bar = factory.Create<IBar>();

// no need to call Verifiable() on the setup
// as we'll be validating all of them anyway.
foo.Setup(f => f.Do());
bar.Setup(b => b.Redo());

// exercise the mocks here

factory.VerifyAll();
// At this point all setups are already checked
// and an optional MockException might be thrown.
// Note also that because the mocks are strict, any invocation
// that doesn't have a matching setup will also throw a MockException.
 The following examples shows how to setup the factory to create loose mocks and later verify only verifiable setups:
CopyC#
var factory = new MockFactory(MockBehavior.Loose);

var foo = factory.Create<IFoo>();
var bar = factory.Create<IBar>();

// this setup will be verified when we verify the factory
foo.Setup(f => f.Do()).Verifiable();

// this setup will NOT be verified
foo.Setup(f => f.Calculate());

// this setup will be verified when we verify the factory
bar.Setup(b => b.Redo()).Verifiable();

// exercise the mocks here
// note that because the mocks are Loose, members
// called in the interfaces for which no matching
// setups exist will NOT throw exceptions,
// and will rather return default values.

factory.Verify();
// At this point verifiable setups are already checked
// and an optional MockException might be thrown.
 The following examples shows how to setup the factory with a default strict behavior, overriding that default for a specific mock:
CopyC#
var factory = new MockFactory(MockBehavior.Strict);

// this particular one we want loose
var foo = factory.Create<IFoo>(MockBehavior.Loose);
var bar = factory.Create<IBar>();

// specify setups

// exercise the mocks here

factory.Verify();

Inheritance Hierarchy

Object
Moq..::.MockFactory

See Also

MockFactory Members
Moq..::.MockBehavior


MockFactory Members

[image]

Moq
MockFactory Members
MockFactory Class Constructors Methods Properties See Also Send Feedback

The MockFactory type exposes the following members.

Constructors

Name | Description
MockFactory | Initializes the factory with the given defaultBehavior for newly created mocks from the factory.

Methods

Name | Description
Create | Overloaded.
Verify | Verifies all verifiable expectations on all mocks created by this factory.
VerifyAll | Verifies all verifiable expectations on all mocks created by this factory.

Properties

Name | Description
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.

See Also

MockFactory Class


MockFactory Constructor

MockFactory Constructor
MockFactory Class See Also Send Feedback
Initializes the factory with the given defaultBehavior for newly created mocks from the factory.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public MockFactory(
    MockBehavior defaultBehavior
)

Parameters

defaultBehavior
Type: Moq..::.MockBehavior
The behavior to use for mocks created using the Create<(Of <(T>)>)()()() factory method if not overriden by using the Create<(Of <(T>)>)(MockBehavior) overload.

See Also

MockFactory Class


MockFactory Methods

[image]

Moq
MockFactory Methods
MockFactory Class See Also Send Feedback

The MockFactory type exposes the following members.

Methods

Name | Description
Create | Overloaded.
Verify | Verifies all verifiable expectations on all mocks created by this factory.
VerifyAll | Verifies all verifiable expectations on all mocks created by this factory.

See Also

MockFactory Class


Create Method

[image]

Moq
MockFactory..::.Create Method
MockFactory Class See Also Send Feedback

Overload List

Name | Description
Create<(Of <(T>)>)()()() | Creates a new mock with the default MockBehavior specified at factory construction time.
Create<(Of <(T>)>)(MockBehavior) | Creates a new mock with the given behavior.
Create<(Of <(T>)>)(array<Object>[]()[]) | Creates a new mock with the default MockBehavior specified at factory construction time and with the the given constructor arguments for the class.
Create<(Of <(T>)>)(MockBehavior, array<Object>[]()[]) | Creates a new mock with the given behavior and with the the given constructor arguments for the class.

See Also

MockFactory Class
MockFactory Members


Create(T) Method

MockFactory..::.Create<(Of <(T>)>) Method
MockFactory Class Example See Also Send Feedback
Creates a new mock with the default MockBehavior specified at factory construction time.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock<T> Create<T>()
where T : class

Type Parameters

T
Type to mock.

Return Value

A new Mock<(Of <(T>)>).

Examples

CopyC#
var factory = new MockFactory(MockBehavior.Strict);

var foo = factory.Create<IFoo>();
// use mock on tests

factory.VerifyAll();

See Also

MockFactory Class
Create Overload


Create(T) Method (MockBehavior)

MockFactory..::.Create<(Of <(T>)>) Method (MockBehavior)
MockFactory Class Example See Also Send Feedback
Creates a new mock with the given behavior.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock<T> Create<T>(
    MockBehavior behavior
)
where T : class

Parameters

behavior
Type: Moq..::.MockBehavior
Behavior to use for the mock, which overrides the default behavior specified at factory construction time.

Type Parameters

T
Type to mock.

Return Value

A new Mock<(Of <(T>)>).

Examples

The following example shows how to create a mock with a different behavior to that specified as the default for the factory:
CopyC#
var factory = new MockFactory(MockBehavior.Strict);

var foo = factory.Create<IFoo>(MockBehavior.Loose);

See Also

MockFactory Class
Create Overload


Create(T) Method ([])

MockFactory..::.Create<(Of <(T>)>) Method (array<>[]()[])
MockFactory Class Example See Also Send Feedback
Creates a new mock with the default MockBehavior specified at factory construction time and with the the given constructor arguments for the class.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock<T> Create<T>(
    params Object[] args
)
where T : class

Parameters

args
Type: array< Object >[]()[]
Constructor arguments for mocked classes.

Type Parameters

T
Type to mock.

Return Value

A new Mock<(Of <(T>)>).

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.

Examples

CopyC#
var factory = new MockFactory(MockBehavior.Default);

var mock = factory.Create<MyBase>("Foo", 25, true);
// use mock on tests

factory.Verify();

See Also

MockFactory Class
Create Overload


Create(T) Method (MockBehavior, [])

MockFactory..::.Create<(Of <(T>)>) Method (MockBehavior, array<>[]()[])
MockFactory Class Example See Also Send Feedback
Creates a new mock with the given behavior and with the the given constructor arguments for the class.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public Mock<T> Create<T>(
    MockBehavior behavior,
    params Object[] args
)
where T : class

Parameters

behavior
Type: Moq..::.MockBehavior
Behavior to use for the mock, which overrides the default behavior specified at factory construction time.
args
Type: array< Object >[]()[]
Constructor arguments for mocked classes.

Type Parameters

T
Type to mock.

Return Value

A new Mock<(Of <(T>)>).

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.

Examples

The following example shows how to create a mock with a different behavior to that specified as the default for the factory, passing constructor arguments:
CopyC#
var factory = new MockFactory(MockBehavior.Default);

var mock = factory.Create<MyBase>(MockBehavior.Strict, "Foo", 25, true);

See Also

MockFactory Class
Create Overload


Verify Method

MockFactory..::.Verify Method
MockFactory Class See Also Send Feedback
Verifies all verifiable expectations on all mocks created by this factory.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual void Verify()

Exceptions

Exception | Condition
Moq..::.MockException | One or more mocks had expectations that were not satisfied.

See Also

MockFactory Class
Moq Namespace
Mock..::.Verify()()()


VerifyAll Method

MockFactory..::.VerifyAll Method
MockFactory Class See Also Send Feedback
Verifies all verifiable expectations on all mocks created by this factory.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public virtual void VerifyAll()

Exceptions

Exception | Condition
Moq..::.MockException | One or more mocks had expectations that were not satisfied.

See Also

MockFactory Class
Moq Namespace
Mock..::.Verify()()()


MockFactory Properties

[image]

Moq
MockFactory Properties
MockFactory Class See Also Send Feedback

The MockFactory type exposes the following members.

Properties

Name | Description
CallBase | Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).
DefaultValue | Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.

See Also

MockFactory Class


CallBase Property

MockFactory..::.CallBase Property
MockFactory Class See Also Send Feedback
Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to falseFalsefalsefalse (False in Visual Basic).

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public bool CallBase { get; set; }

See Also

MockFactory Class


DefaultValue Property

MockFactory..::.DefaultValue Property
MockFactory Class See Also Send Feedback
Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public DefaultValue DefaultValue { get; set; }

See Also

MockFactory Class


Mocks Class

Mocks Class
Members See Also Send Feedback
Allows querying the universe of mocks for those that behave according to the query specification.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class Mocks

Inheritance Hierarchy

Object
Moq..::.Mocks

See Also

Mocks Members


Mocks Members

[image]

Moq
Mocks Members
Mocks Class Methods See Also Send Feedback

The Mocks type exposes the following members.

Methods

Name | Description
CreateReal<(Of <(T>)>) | Method that is turned into the actual call from .Query{T}, to transform the queryable query into a normal enumerable query. This method should not be used by consumers.
Query<(Of <(T>)>) | Creates a query for mocks of the given type.

See Also

Mocks Class


Mocks Methods

[image]

Moq
Mocks Methods
Mocks Class See Also Send Feedback

The Mocks type exposes the following members.

Methods

Name | Description
CreateReal<(Of <(T>)>) | Method that is turned into the actual call from .Query{T}, to transform the queryable query into a normal enumerable query. This method should not be used by consumers.
Query<(Of <(T>)>) | Creates a query for mocks of the given type.

See Also

Mocks Class


CreateReal(T) Method

Mocks..::.CreateReal<(Of <(T>)>) Method
Mocks Class See Also Send Feedback
Method that is turned into the actual call from .Query{T}, to transform the queryable query into a normal enumerable query. This method should not be used by consumers.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static IEnumerable<T> CreateReal<T>()
where T : class

Type Parameters

T

Return Value

See Also

Mocks Class


Query(T) Method

Mocks..::.Query<(Of <(T>)>) Method
Mocks Class See Also Send Feedback
Creates a query for mocks of the given type.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static IQueryable<T> Query<T>()

Type Parameters

T
The type of mocked object to query.

See Also

Mocks Class


QueryableMockExtensions Class

QueryableMockExtensions Class
Members See Also Send Feedback
Helper extensions that are used by the query translator.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class QueryableMockExtensions

Inheritance Hierarchy

Object
Moq..::.QueryableMockExtensions

See Also

QueryableMockExtensions Members


QueryableMockExtensions Members

[image]

Moq
QueryableMockExtensions Members
QueryableMockExtensions Class Methods See Also Send Feedback

The QueryableMockExtensions type exposes the following members.

Methods

Name | Description
FluentMock<(Of <(T, TResult>)>) | Retrieves a fluent mock from the given setup expression.

See Also

QueryableMockExtensions Class


QueryableMockExtensions Methods

[image]

Moq
QueryableMockExtensions Methods
QueryableMockExtensions Class See Also Send Feedback

The QueryableMockExtensions type exposes the following members.

Methods

Name | Description
FluentMock<(Of <(T, TResult>)>) | Retrieves a fluent mock from the given setup expression.

See Also

QueryableMockExtensions Class


FluentMock(T, TResult) Method

QueryableMockExtensions..::.FluentMock<(Of <(T, TResult>)>) Method
QueryableMockExtensions Class See Also Send Feedback
Retrieves a fluent mock from the given setup expression.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Mock<TResult> FluentMock<T, TResult>(
    Mock<T> mock,
    Expression<Func<T, TResult>> setup
)
where T : class
where TResult : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)

[Missing <param name="mock"/> documentation for "M:Moq.QueryableMockExtensions.FluentMock``2(Moq.Mock{``0},System.Linq.Expressions.Expression{System.Func{``0,``1}})"]

setup
Type: Expression<(Of <(Func<(Of <(T, TResult>)>)>)>)

[Missing <param name="setup"/> documentation for "M:Moq.QueryableMockExtensions.FluentMock``2(Moq.Mock{``0},System.Linq.Expressions.Expression{System.Func{``0,``1}})"]

Type Parameters

T
TResult

See Also

QueryableMockExtensions Class


Range Enumeration

Range Enumeration
See Also Send Feedback
Kind of range to use in a filter specified through IsInRange<(Of <(TValue>)>)(TValue, TValue, Range).

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public enum Range

Members

Member name | Description
Inclusive | The range includes the to and from values.
Exclusive | The range does not include the to and from values.

See Also


Times Class

Times Class
Members See Also Send Feedback
Defines the number of invocations allowed by a mocked method.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public class Times

Inheritance Hierarchy

Object
Moq..::.Times

See Also

Times Members


Times Members

[image]

Moq
Times Members
Times Class Methods See Also Send Feedback

The Times type exposes the following members.

Methods

Name | Description
AtLeast | Specifies that a mocked method should be invoked times times as minimum.
AtLeastOnce | Specifies that a mocked method should be invoked one time as minimum.
AtMost | Specifies that a mocked method should be invoked times time as maximun.
AtMostOnce | Specifies that a mocked method should be invoked one time as maximun.
Between | Specifies that a mocked method should be invoked between from and to times.
Exactly | Specifies that a mocked method should be invoked exactly times times.
Never | Specifies that a mocked method should not be invoked.
Once | Specifies that a mocked method should be invoked exactly one time.

See Also

Times Class


Times Methods

[image]

Moq
Times Methods
Times Class See Also Send Feedback

The Times type exposes the following members.

Methods

Name | Description
AtLeast | Specifies that a mocked method should be invoked times times as minimum.
AtLeastOnce | Specifies that a mocked method should be invoked one time as minimum.
AtMost | Specifies that a mocked method should be invoked times time as maximun.
AtMostOnce | Specifies that a mocked method should be invoked one time as maximun.
Between | Specifies that a mocked method should be invoked between from and to times.
Exactly | Specifies that a mocked method should be invoked exactly times times.
Never | Specifies that a mocked method should not be invoked.
Once | Specifies that a mocked method should be invoked exactly one time.

See Also

Times Class


AtLeast Method

Times..::.AtLeast Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked times times as minimum.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times AtLeast(
    int callCount
)

Parameters

callCount
Type: Int32
The minimun number of times.

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


AtLeastOnce Method

Times..::.AtLeastOnce Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked one time as minimum.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times AtLeastOnce()

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


AtMost Method

Times..::.AtMost Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked times time as maximun.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times AtMost(
    int callCount
)

Parameters

callCount
Type: Int32
The maximun number of times.

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


AtMostOnce Method

Times..::.AtMostOnce Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked one time as maximun.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times AtMostOnce()

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


Between Method

Times..::.Between Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked between from and to times.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times Between(
    int callCountFrom,
    int callCountTo,
    Range rangeKind
)

Parameters

callCountFrom
Type: Int32
The minimun number of times.
callCountTo
Type: Int32
The maximun number of times.
rangeKind
Type: Moq..::.Range
The kind of range. See Range.

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


Exactly Method

Times..::.Exactly Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked exactly times times.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times Exactly(
    int callCount
)

Parameters

callCount
Type: Int32
The times that a method or property can be called.

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


Never Method

Times..::.Never Method
Times Class See Also Send Feedback
Specifies that a mocked method should not be invoked.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times Never()

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


Once Method

Times..::.Once Method
Times Class See Also Send Feedback
Specifies that a mocked method should be invoked exactly one time.

Namespace: Moq
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Times Once()

Return Value

An object defining the allowed number of invocations.

See Also

Times Class


Moq.Language Namespace

Moq.Language Namespace
Send Feedback
This namespace defines the interfaces that contain the methods available in Moq fluent API, such as Setup, Callback, Returns, Throws, etc.

Review the documentation of the available methods on these interfaces for code examples. The way these interfaces are composed, grouped and made visible at different stages during an expectation (i.e. Verifiable is the last "verb" and can't be specified before the Returns) is internal but the API will naturally lead you to the proper way of using it, so don't worry too much about who (and where) exposes these language interfaces. They will show up in Intellisense when it's appropriate.

Do use their documentation to learn about options (especially when there are several overloads available) that may be a better fit for a particular scenario.

Interfaces

Interface | Description
ICallback | Defines the Callback verb and overloads.
ICallback<(Of <(TMock, TResult>)>) | Defines the Callback verb and overloads for callbacks on setups that return a value.
ICallbackGetter<(Of <(TMock, TProperty>)>) | Defines the Callback verb for property getter setups.
ICallbackSetter<(Of <(TProperty>)>) | Defines the Callback verb for property setter setups.
IRaise | Defines the Raises verb.
IRaise<(Of <(T>)>) | Defines the Raises verb.
IReturns<(Of <(TMock, TResult>)>) | Defines the Returns verb.
IReturnsGetter<(Of <(TMock, TProperty>)>) | Defines the Returns verb for property get setups.
IThrows | Defines the Throws verb.
IVerifies | Defines the Verifiable verb.


ICallback Interface

ICallback Interface
Members See Also Send Feedback
Defines the Callback verb and overloads.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface ICallback

See Also

ICallback Members
Moq.Language Namespace


ICallback Members

[image]

Moq
ICallback Members
ICallback Interface Methods See Also Send Feedback

Methods

Name | Description
Callback | Overloaded.

See Also

ICallback Interface
Moq.Language Namespace


ICallback Methods

[image]

Moq
ICallback Methods
ICallback Interface See Also Send Feedback

Methods

Name | Description
Callback | Overloaded.

See Also

ICallback Interface
Moq.Language Namespace


Callback Method

[image]

Moq
ICallback..::.Callback Method
ICallback Interface See Also Send Feedback

Overload List

Name | Description
Callback(Action) | Specifies a callback to invoke when the method is called.
Callback<(Of <(T>)>)(Action<(Of <(T>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2>)>)(Action<(Of <(T1, T2>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2, T3>)>)(Action<(Of <(T1, T2, T3>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2, T3, T4>)>)(Action<(Of <(T1, T2, T3, T4>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.

See Also

ICallback Interface
ICallback Members
Moq.Language Namespace


Callback Method ()

ICallback..::.Callback Method ()
ICallback Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback(
    Action action
)

Parameters

action
Type: Action
Callback method to invoke.

Examples

The following example specifies a callback to set a boolean value that can be used later:
CopyC#
bool called = false;
mock.Setup(x => x.Execute())
    .Callback(() => called = true);

See Also

ICallback Interface
Callback Overload
Moq.Language Namespace


Callback(T) Method ((T))

ICallback..::.Callback<(Of <(T>)>) Method (<(Of <(T>)>))
ICallback Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback<T>(
    Action<T> action
)

Parameters

action
Type: Action<(Of <(T>)>)
Callback method to invoke.

Type Parameters

T
Argument type of the invoked method.

Examples

Invokes the given callback with the concrete invocation argument value.

Notice how the specific string argument is retrieved by simply declaring it as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(It.IsAny<string>()))
    .Callback((string command) => Console.WriteLine(command));

See Also

ICallback Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2) Method ((T1, T2))

ICallback..::.Callback<(Of <(T1, T2>)>) Method (<(Of <(T1, T2>)>))
ICallback Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback<T1, T2>(
    Action<T1, T2> action
)

Parameters

action
Type: Action<(Of <(T1, T2>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>()))
    .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2));

See Also

ICallback Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2, T3) Method ((T1, T2, T3))

ICallback..::.Callback<(Of <(T1, T2, T3>)>) Method (<(Of <(T1, T2, T3>)>))
ICallback Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback<T1, T2, T3>(
    Action<T1, T2, T3> action
)

Parameters

action
Type: Action<(Of <(T1, T2, T3>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>(),
                     It.IsAny<int>()))
    .Callback((string arg1, string arg2, int arg3) => Console.WriteLine(arg1 + arg2 + arg3));

See Also

ICallback Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2, T3, T4) Method ((T1, T2, T3, T4))

ICallback..::.Callback<(Of <(T1, T2, T3, T4>)>) Method (<(Of <(T1, T2, T3, T4>)>))
ICallback Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback<T1, T2, T3, T4>(
    Action<T1, T2, T3, T4> action
)

Parameters

action
Type: Action<(Of <(T1, T2, T3, T4>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.
T4
Type of the fourth argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>(),
                     It.IsAny<int>(),
                     It.IsAny<bool>()))
    .Callback((string arg1, string arg2, int arg3, bool arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));

See Also

ICallback Interface
Callback Overload
Moq.Language Namespace


ICallback(TMock, TResult) Interface

ICallback<(Of <(TMock, TResult>)>) Interface
Members See Also Send Feedback
Defines the Callback verb and overloads for callbacks on setups that return a value.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface ICallback<TMock, TResult>
where TMock : class

Type Parameters

TMock
Mocked type.
TResult
Type of the return value of the setup.

See Also

ICallback<(Of <(TMock, TResult>)>) Members
Moq.Language Namespace


ICallback(TMock, TResult) Members

[image]

Moq
ICallback<(Of <(TMock, TResult>)>) Members
ICallback<(Of <(TMock, TResult>)>) Interface Methods See Also Send Feedback

Methods

Name | Description
Callback | Overloaded.

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Moq.Language Namespace


ICallback(TMock, TResult) Methods

[image]

Moq
ICallback<(Of <(TMock, TResult>)>) Methods
ICallback<(Of <(TMock, TResult>)>) Interface See Also Send Feedback

Methods

Name | Description
Callback | Overloaded.

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Moq.Language Namespace


Callback Method

[image]

Moq
ICallback<(Of <(TMock, TResult>)>)..::.Callback Method
ICallback<(Of <(TMock, TResult>)>) Interface See Also Send Feedback

Overload List

Name | Description
Callback(Action) | Specifies a callback to invoke when the method is called.
Callback<(Of <(T>)>)(Action<(Of <(T>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2>)>)(Action<(Of <(T1, T2>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2, T3>)>)(Action<(Of <(T1, T2, T3>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.
Callback<(Of <(T1, T2, T3, T4>)>)(Action<(Of <(T1, T2, T3, T4>)>)) | Specifies a callback to invoke when the method is called that receives the original arguments.

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
ICallback<(Of <(TMock, TResult>)>) Members
Moq.Language Namespace


Callback Method ()

ICallback<(Of <(TMock, TResult>)>)..::.Callback Method ()
ICallback<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrows<TMock, TResult> Callback(
    Action action
)

Parameters

action
Type: Action
Callback method to invoke.

Examples

The following example specifies a callback to set a boolean value that can be used later:
CopyC#
bool called = false;
mock.Setup(x => x.Execute())
    .Callback(() => called = true)
    .Returns(true);
 Note that in the case of value-returning methods, after the Callback call you can still specify the return value.

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Callback Overload
Moq.Language Namespace


Callback(T) Method ((T))

ICallback<(Of <(TMock, TResult>)>)..::.Callback<(Of <(T>)>) Method (<(Of <(T>)>))
ICallback<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrows<TMock, TResult> Callback<T>(
    Action<T> action
)

Parameters

action
Type: Action<(Of <(T>)>)
Callback method to invoke.

Type Parameters

T
Type of the argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation argument value.

Notice how the specific string argument is retrieved by simply declaring it as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(It.IsAny<string>()))
    .Callback((string command) => Console.WriteLine(command))
    .Returns(true);

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2) Method ((T1, T2))

ICallback<(Of <(TMock, TResult>)>)..::.Callback<(Of <(T1, T2>)>) Method (<(Of <(T1, T2>)>))
ICallback<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrows<TMock, TResult> Callback<T1, T2>(
    Action<T1, T2> action
)

Parameters

action
Type: Action<(Of <(T1, T2>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>()))
    .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2))
    .Returns(true);

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2, T3) Method ((T1, T2, T3))

ICallback<(Of <(TMock, TResult>)>)..::.Callback<(Of <(T1, T2, T3>)>) Method (<(Of <(T1, T2, T3>)>))
ICallback<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrows<TMock, TResult> Callback<T1, T2, T3>(
    Action<T1, T2, T3> action
)

Parameters

action
Type: Action<(Of <(T1, T2, T3>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>(),
                     It.IsAny<int>()))
    .Callback((string arg1, string arg2, int arg3) => Console.WriteLine(arg1 + arg2 + arg3))
    .Returns(true);

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Callback Overload
Moq.Language Namespace


Callback(T1, T2, T3, T4) Method ((T1, T2, T3, T4))

ICallback<(Of <(TMock, TResult>)>)..::.Callback<(Of <(T1, T2, T3, T4>)>) Method (<(Of <(T1, T2, T3, T4>)>))
ICallback<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the method is called that receives the original arguments.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrows<TMock, TResult> Callback<T1, T2, T3, T4>(
    Action<T1, T2, T3, T4> action
)

Parameters

action
Type: Action<(Of <(T1, T2, T3, T4>)>)
Callback method to invoke.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.
T4
Type of the fourth argument of the invoked method.

Examples

Invokes the given callback with the concrete invocation arguments values.

Notice how the specific arguments are retrieved by simply declaring them as part of the lambda expression for the callback:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>(),
                     It.IsAny<int>(),
                     It.IsAny<bool>()))
    .Callback((string arg1, string arg2, int arg3, bool arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4))
    .Returns(true);

See Also

ICallback<(Of <(TMock, TResult>)>) Interface
Callback Overload
Moq.Language Namespace


ICallbackGetter(TMock, TProperty) Interface

ICallbackGetter<(Of <(TMock, TProperty>)>) Interface
Members See Also Send Feedback
Defines the Callback verb for property getter setups.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface ICallbackGetter<TMock, TProperty>
where TMock : class

Type Parameters

TMock
Mocked type.
TProperty
Type of the property.

See Also

ICallbackGetter<(Of <(TMock, TProperty>)>) Members
Moq.Language Namespace
Mock<(Of <(T>)>)..::.SetupGet<(Of <(TProperty>)>)(Expression<(Of <(Func<(Of <(T, TProperty>)>)>)>))


ICallbackGetter(TMock, TProperty) Members

[image]

Moq
ICallbackGetter<(Of <(TMock, TProperty>)>) Members
ICallbackGetter<(Of <(TMock, TProperty>)>) Interface Methods See Also Send Feedback

The ICallbackGetter<(Of <(TMock, TProperty>)>) type exposes the following members.

Methods

Name | Description
Callback | Specifies a callback to invoke when the property is retrieved.

See Also

ICallbackGetter<(Of <(TMock, TProperty>)>) Interface
Moq.Language Namespace


ICallbackGetter(TMock, TProperty) Methods

[image]

Moq
ICallbackGetter<(Of <(TMock, TProperty>)>) Methods
ICallbackGetter<(Of <(TMock, TProperty>)>) Interface See Also Send Feedback

The ICallbackGetter<(Of <(TMock, TProperty>)>) type exposes the following members.

Methods

Name | Description
Callback | Specifies a callback to invoke when the property is retrieved.

See Also

ICallbackGetter<(Of <(TMock, TProperty>)>) Interface
Moq.Language Namespace


Callback Method

ICallbackGetter<(Of <(TMock, TProperty>)>)..::.Callback Method
ICallbackGetter<(Of <(TMock, TProperty>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the property is retrieved.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsThrowsGetter<TMock, TProperty> Callback(
    Action action
)

Parameters

action
Type: Action
Callback method to invoke.

Examples

Invokes the given callback with the property value being set.
CopyC#
mock.SetupGet(x => x.Suspended)
    .Callback(() => called = true)
    .Returns(true);

See Also

ICallbackGetter<(Of <(TMock, TProperty>)>) Interface
Moq.Language Namespace


ICallbackSetter(TProperty) Interface

ICallbackSetter<(Of <(TProperty>)>) Interface
Members See Also Send Feedback
Defines the Callback verb for property setter setups.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface ICallbackSetter<TProperty>

Type Parameters

TProperty
Type of the property.

See Also

ICallbackSetter<(Of <(TProperty>)>) Members
Moq.Language Namespace


ICallbackSetter(TProperty) Members

[image]

Moq
ICallbackSetter<(Of <(TProperty>)>) Members
ICallbackSetter<(Of <(TProperty>)>) Interface Methods See Also Send Feedback

The ICallbackSetter<(Of <(TProperty>)>) type exposes the following members.

Methods

Name | Description
Callback | Specifies a callback to invoke when the property is set that receives the property value being set.

See Also

ICallbackSetter<(Of <(TProperty>)>) Interface
Moq.Language Namespace


ICallbackSetter(TProperty) Methods

[image]

Moq
ICallbackSetter<(Of <(TProperty>)>) Methods
ICallbackSetter<(Of <(TProperty>)>) Interface See Also Send Feedback

The ICallbackSetter<(Of <(TProperty>)>) type exposes the following members.

Methods

Name | Description
Callback | Specifies a callback to invoke when the property is set that receives the property value being set.

See Also

ICallbackSetter<(Of <(TProperty>)>) Interface
Moq.Language Namespace


Callback Method

ICallbackSetter<(Of <(TProperty>)>)..::.Callback Method
ICallbackSetter<(Of <(TProperty>)>) Interface Example See Also Send Feedback
Specifies a callback to invoke when the property is set that receives the property value being set.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ICallbackResult Callback(
    Action<TProperty> action
)

Parameters

action
Type: Action<(Of <( TProperty >)>)
Callback method to invoke.

Examples

Invokes the given callback with the property value being set.
CopyC#
mock.SetupSet(x => x.Suspended)
    .Callback((bool state) => Console.WriteLine(state));

See Also

ICallbackSetter<(Of <(TProperty>)>) Interface
Moq.Language Namespace


IRaise Interface

IRaise Interface
Members See Also Send Feedback
Defines the Raises verb.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IRaise

See Also

IRaise Members
Moq.Language Namespace


IRaise Members

[image]

Moq
IRaise Members
IRaise Interface Methods See Also Send Feedback

Methods

Name | Description
Raises | Overloaded.

See Also

IRaise Interface
Moq.Language Namespace


IRaise Methods

[image]

Moq
IRaise Methods
IRaise Interface See Also Send Feedback

Methods

Name | Description
Raises | Overloaded.

See Also

IRaise Interface
Moq.Language Namespace


Raises Method

[image]

Moq
IRaise..::.Raises Method
IRaise Interface See Also Send Feedback

Overload List

Name | Description
Raises(MockedEvent, EventArgs) | Specifies the mocked event that will be raised when the setup is met.
Raises(MockedEvent, Func<(Of <(EventArgs>)>)) | Specifies the mocked event that will be raised when the setup is matched.
Raises<(Of <(T>)>)(MockedEvent, Func<(Of <(T, EventArgs>)>)) | Specifies the mocked event that will be raised when the setup is matched.
Raises<(Of <(T1, T2>)>)(MockedEvent, Func<(Of <(T1, T2, EventArgs>)>)) | Specifies the mocked event that will be raised when the setup is matched.
Raises<(Of <(T1, T2, T3>)>)(MockedEvent, Func<(Of <(T1, T2, T3, EventArgs>)>)) | Specifies the mocked event that will be raised when the setup is matched.
Raises<(Of <(T1, T2, T3, T4>)>)(MockedEvent, Func<(Of <(T1, T2, T3, T4, EventArgs>)>)) | Specifies the mocked event that will be raised when the setup is matched.

See Also

IRaise Interface
IRaise Members
Moq.Language Namespace


Raises Method (, )

IRaise..::.Raises Method (, )
IRaise Interface Example See Also Send Feedback
Specifies the mocked event that will be raised when the setup is met.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises(
    MockedEvent eventHandler,
    EventArgs args
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
args
Type: EventArgs
The event args to pass when raising the event.

Examples

The following example shows how to raise an event when the setup is met:
CopyC#
var mock = new Mock<IContainer>();
// create handler to associate with the event to raise
var handler = mock.CreateEventHandler();
// associate the handler with the event to raise
mock.Object.Added += handler;
// setup the invocation and the handler to raise
mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
    .Raises(handler, EventArgs.Empty);

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace


Raises Method (, ())

IRaise..::.Raises Method (, <(Of <(>)>))
IRaise Interface See Also Send Feedback
Specifies the mocked event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises(
    MockedEvent eventHandler,
    Func<EventArgs> func
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
func
Type: Func<(Of <(EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace
IRaise..::.Raises(MockedEvent, EventArgs)


Raises(T) Method (, (T, ))

IRaise..::.Raises<(Of <(T>)>) Method (, <(Of <(T, >)>))
IRaise Interface See Also Send Feedback
Specifies the mocked event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T>(
    MockedEvent eventHandler,
    Func<T, EventArgs> func
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
func
Type: Func<(Of <(T, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T
Type of the argument received by the expected invocation.

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace
IRaise..::.Raises(MockedEvent, EventArgs)


Raises(T1, T2) Method (, (T1, T2, ))

IRaise..::.Raises<(Of <(T1, T2>)>) Method (, <(Of <(T1, T2, >)>))
IRaise Interface See Also Send Feedback
Specifies the mocked event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2>(
    MockedEvent eventHandler,
    Func<T1, T2, EventArgs> func
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
func
Type: Func<(Of <(T1, T2, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace
IRaise..::.Raises(MockedEvent, EventArgs)


Raises(T1, T2, T3) Method (, (T1, T2, T3, ))

IRaise..::.Raises<(Of <(T1, T2, T3>)>) Method (, <(Of <(T1, T2, T3, >)>))
IRaise Interface See Also Send Feedback
Specifies the mocked event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2, T3>(
    MockedEvent eventHandler,
    Func<T1, T2, T3, EventArgs> func
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
func
Type: Func<(Of <(T1, T2, T3, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.
T3
Type of the third argument received by the expected invocation.

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace
IRaise..::.Raises(MockedEvent, EventArgs)


Raises(T1, T2, T3, T4) Method (, (T1, T2, T3, T4, ))

IRaise..::.Raises<(Of <(T1, T2, T3, T4>)>) Method (, <(Of <(T1, T2, T3, T4, >)>))
IRaise Interface See Also Send Feedback
Specifies the mocked event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2, T3, T4>(
    MockedEvent eventHandler,
    Func<T1, T2, T3, T4, EventArgs> func
)

Parameters

eventHandler
Type: MockedEvent
The mocked event, retrieved from CreateEventHandler()()() or CreateEventHandler``1()()().
func
Type: Func<(Of <(T1, T2, T3, T4, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.
T3
Type of the third argument received by the expected invocation.
T4
Type of the fourth argument received by the expected invocation.

See Also

IRaise Interface
Raises Overload
Moq.Language Namespace
IRaise..::.Raises(MockedEvent, EventArgs)


IRaise(T) Interface

IRaise<(Of <(T>)>) Interface
Members See Also Send Feedback
Defines the Raises verb.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IRaise<T>

Type Parameters

T

See Also

IRaise<(Of <(T>)>) Members
Moq.Language Namespace


IRaise(T) Members

[image]

Moq
IRaise<(Of <(T>)>) Members
IRaise<(Of <(T>)>) Interface Methods See Also Send Feedback

Methods

Name | Description
Raises | Overloaded.

See Also

IRaise<(Of <(T>)>) Interface
Moq.Language Namespace


IRaise(T) Methods

[image]

Moq
IRaise<(Of <(T>)>) Methods
IRaise<(Of <(T>)>) Interface See Also Send Feedback

Methods

Name | Description
Raises | Overloaded.

See Also

IRaise<(Of <(T>)>) Interface
Moq.Language Namespace


Raises Method

[image]

Moq
IRaise<(Of <(T>)>)..::.Raises Method
IRaise<(Of <(T>)>) Interface See Also Send Feedback

Overload List

Name | Description
Raises(Action<(Of <(T>)>), EventArgs) | Specifies the event that will be raised when the setup is met.
Raises(Action<(Of <(T>)>), Func<(Of <(EventArgs>)>)) | Specifies the event that will be raised when the setup is matched.
Raises<(Of <(T1>)>)(Action<(Of <(T>)>), Func<(Of <(T1, EventArgs>)>)) | Specifies the event that will be raised when the setup is matched.
Raises<(Of <(T1, T2>)>)(Action<(Of <(T>)>), Func<(Of <(T1, T2, EventArgs>)>)) | Specifies the event that will be raised when the setup is matched.
Raises<(Of <(T1, T2, T3>)>)(Action<(Of <(T>)>), Func<(Of <(T1, T2, T3, EventArgs>)>)) | Specifies the event that will be raised when the setup is matched.
Raises<(Of <(T1, T2, T3, T4>)>)(Action<(Of <(T>)>), Func<(Of <(T1, T2, T3, T4, EventArgs>)>)) | Specifies the event that will be raised when the setup is matched.
Raises(Action<(Of <(T>)>), array<Object>[]()[]) | Specifies the custom event that will be raised when the setup is matched.

See Also

IRaise<(Of <(T>)>) Interface
IRaise<(Of <(T>)>) Members
Moq.Language Namespace


Raises Method ((T), )

IRaise<(Of <(T>)>)..::.Raises Method (<(Of <(T>)>), )
IRaise<(Of <(T>)>) Interface Example See Also Send Feedback
Specifies the event that will be raised when the setup is met.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises(
    Action<T> eventExpression,
    EventArgs args
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
args
Type: EventArgs
The event arguments to pass for the raised event.

Examples

The following example shows how to raise an event when the setup is met:
CopyC#
var mock = new Mock<IContainer>();

mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
    .Raises(add => add.Added += null, EventArgs.Empty);

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace


Raises Method ((T), ())

IRaise<(Of <(T>)>)..::.Raises Method (<(Of <(T>)>), <(Of <(>)>))
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises(
    Action<T> eventExpression,
    Func<EventArgs> func
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
func
Type: Func<(Of <(EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace
IRaise<(Of <(T>)>)..::.Raises(Action<(Of <(T>)>), EventArgs)


Raises(T1) Method ((T), (T1, ))

IRaise<(Of <(T>)>)..::.Raises<(Of <(T1>)>) Method (<(Of <(T>)>), <(Of <(T1, >)>))
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1>(
    Action<T> eventExpression,
    Func<T1, EventArgs> func
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
func
Type: Func<(Of <(T1, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the argument received by the expected invocation.

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace
IRaise<(Of <(T>)>)..::.Raises(Action<(Of <(T>)>), EventArgs)


Raises(T1, T2) Method ((T), (T1, T2, ))

IRaise<(Of <(T>)>)..::.Raises<(Of <(T1, T2>)>) Method (<(Of <(T>)>), <(Of <(T1, T2, >)>))
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2>(
    Action<T> eventExpression,
    Func<T1, T2, EventArgs> func
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
func
Type: Func<(Of <(T1, T2, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace
IRaise<(Of <(T>)>)..::.Raises(Action<(Of <(T>)>), EventArgs)


Raises(T1, T2, T3) Method ((T), (T1, T2, T3, ))

IRaise<(Of <(T>)>)..::.Raises<(Of <(T1, T2, T3>)>) Method (<(Of <(T>)>), <(Of <(T1, T2, T3, >)>))
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2, T3>(
    Action<T> eventExpression,
    Func<T1, T2, T3, EventArgs> func
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
func
Type: Func<(Of <(T1, T2, T3, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.
T3
Type of the third argument received by the expected invocation.

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace
IRaise<(Of <(T>)>)..::.Raises(Action<(Of <(T>)>), EventArgs)


Raises(T1, T2, T3, T4) Method ((T), (T1, T2, T3, T4, ))

IRaise<(Of <(T>)>)..::.Raises<(Of <(T1, T2, T3, T4>)>) Method (<(Of <(T>)>), <(Of <(T1, T2, T3, T4, >)>))
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises<T1, T2, T3, T4>(
    Action<T> eventExpression,
    Func<T1, T2, T3, T4, EventArgs> func
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
func
Type: Func<(Of <(T1, T2, T3, T4, EventArgs>)>)
A function that will build the EventArgs to pass when raising the event.

Type Parameters

T1
Type of the first argument received by the expected invocation.
T2
Type of the second argument received by the expected invocation.
T3
Type of the third argument received by the expected invocation.
T4
Type of the fourth argument received by the expected invocation.

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace
IRaise<(Of <(T>)>)..::.Raises(Action<(Of <(T>)>), EventArgs)


Raises Method ((T), [])

IRaise<(Of <(T>)>)..::.Raises Method (<(Of <(T>)>), array<>[]()[])
IRaise<(Of <(T>)>) Interface See Also Send Feedback
Specifies the custom event that will be raised when the setup is matched.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IVerifies Raises(
    Action<T> eventExpression,
    params Object[] args
)

Parameters

eventExpression
Type: Action<(Of <( T >)>)
An expression that represents an event attach or detach action.
args
Type: array< Object >[]()[]
The arguments to pass to the custom delegate (non EventHandler-compatible).

See Also

IRaise<(Of <(T>)>) Interface
Raises Overload
Moq.Language Namespace


IReturns(TMock, TResult) Interface

IReturns<(Of <(TMock, TResult>)>) Interface
Members See Also Send Feedback
Defines the Returns verb.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IReturns<TMock, TResult>
where TMock : class

Type Parameters

TMock
Mocked type.
TResult
Type of the return value from the expression.

See Also

IReturns<(Of <(TMock, TResult>)>) Members
Moq.Language Namespace


IReturns(TMock, TResult) Members

[image]

Moq
IReturns<(Of <(TMock, TResult>)>) Members
IReturns<(Of <(TMock, TResult>)>) Interface Methods See Also Send Feedback

Methods

Name | Description
Returns | Overloaded.

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Moq.Language Namespace


IReturns(TMock, TResult) Methods

[image]

Moq
IReturns<(Of <(TMock, TResult>)>) Methods
IReturns<(Of <(TMock, TResult>)>) Interface See Also Send Feedback

Methods

Name | Description
Returns | Overloaded.

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Moq.Language Namespace


Returns Method

[image]

Moq
IReturns<(Of <(TMock, TResult>)>)..::.Returns Method
IReturns<(Of <(TMock, TResult>)>) Interface See Also Send Feedback

Overload List

Name | Description
Returns(Func<(Of <(TResult>)>)) | Specifies a function that will calculate the value to return from the method.
Returns<(Of <(T>)>)(Func<(Of <(T, TResult>)>)) | Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.
Returns<(Of <(T1, T2>)>)(Func<(Of <(T1, T2, TResult>)>)) | Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.
Returns<(Of <(T1, T2, T3>)>)(Func<(Of <(T1, T2, T3, TResult>)>)) | Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.
Returns<(Of <(T1, T2, T3, T4>)>)(Func<(Of <(T1, T2, T3, T4, TResult>)>)) | Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.
Returns(TResult) | Specifies the value to return.

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
IReturns<(Of <(TMock, TResult>)>) Members
Moq.Language Namespace


Returns Method ((TResult))

IReturns<(Of <(TMock, TResult>)>)..::.Returns Method (<(Of <(TResult>)>))
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return from the method.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns(
    Func<TResult> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <( TResult >)>)
The function that will calculate the return value.

Examples

Return a calculated value when the method is called:
CopyC#
mock.Setup(x => x.Execute("ping"))
    .Returns(() => returnValues[0]);
 The lambda expression to retrieve the return value is lazy-executed, meaning that its value may change depending on the moment the method is executed and the value the returnValues array has at that moment.

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


Returns(T) Method ((T, TResult))

IReturns<(Of <(TMock, TResult>)>)..::.Returns<(Of <(T>)>) Method (<(Of <(T, TResult>)>))
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns<T>(
    Func<T, TResult> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <(T, TResult >)>)
The function that will calculate the return value.

Type Parameters

T
Type of the argument of the invoked method.

Examples

Return a calculated value which is evaluated lazily at the time of the invocation.

The lookup list can change between invocations and the setup will return different values accordingly. Also, notice how the specific string argument is retrieved by simply declaring it as part of the lambda expression:

CopyC#
mock.Setup(x => x.Execute(It.IsAny<string>()))
    .Returns((string command) => returnValues[command]);

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


Returns(T1, T2) Method ((T1, T2, TResult))

IReturns<(Of <(TMock, TResult>)>)..::.Returns<(Of <(T1, T2>)>) Method (<(Of <(T1, T2, TResult>)>))
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns<T1, T2>(
    Func<T1, T2, TResult> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <(T1, T2, TResult >)>)
The function that will calculate the return value.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.

Examples

Return a calculated value which is evaluated lazily at the time of the invocation.

The return value is calculated from the value of the actual method invocation arguments. Notice how the arguments are retrieved by simply declaring them as part of the lambda expression:

CopyC#
mock.Setup(x => x.Execute(
                    It.IsAny<string>(),
                    It.IsAny<string>()))
    .Returns((string arg1, string arg2) => arg1 + arg2);

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


Returns(T1, T2, T3) Method ((T1, T2, T3, TResult))

IReturns<(Of <(TMock, TResult>)>)..::.Returns<(Of <(T1, T2, T3>)>) Method (<(Of <(T1, T2, T3, TResult>)>))
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns<T1, T2, T3>(
    Func<T1, T2, T3, TResult> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <(T1, T2, T3, TResult >)>)
The function that will calculate the return value.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.

Examples

Return a calculated value which is evaluated lazily at the time of the invocation.

The return value is calculated from the value of the actual method invocation arguments. Notice how the arguments are retrieved by simply declaring them as part of the lambda expression:

CopyC#
mock.Setup(x => x.Execute(
                     It.IsAny<string>(),
                     It.IsAny<string>(),
                     It.IsAny<int>()))
    .Returns((string arg1, string arg2, int arg3) => arg1 + arg2 + arg3);

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


Returns(T1, T2, T3, T4) Method ((T1, T2, T3, T4, TResult))

IReturns<(Of <(TMock, TResult>)>)..::.Returns<(Of <(T1, T2, T3, T4>)>) Method (<(Of <(T1, T2, T3, T4, TResult>)>))
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return from the method, retrieving the arguments for the invocation.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns<T1, T2, T3, T4>(
    Func<T1, T2, T3, T4, TResult> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <(T1, T2, T3, T4, TResult >)>)
The function that will calculate the return value.

Type Parameters

T1
Type of the first argument of the invoked method.
T2
Type of the second argument of the invoked method.
T3
Type of the third argument of the invoked method.
T4
Type of the fourth argument of the invoked method.

Examples

Return a calculated value which is evaluated lazily at the time of the invocation.

The return value is calculated from the value of the actual method invocation arguments. Notice how the arguments are retrieved by simply declaring them as part of the lambda expression:

CopyC#
mock.Setup(x => x.Execute(
                      It.IsAny<string>(),
                      It.IsAny<string>(),
                      It.IsAny<int>(),
                      It.IsAny<bool>()))
    .Returns((string arg1, string arg2, int arg3, bool arg4) => arg1 + arg2 + arg3 + arg4);

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


Returns Method (TResult)

IReturns<(Of <(TMock, TResult>)>)..::.Returns Method (TResult)
IReturns<(Of <(TMock, TResult>)>) Interface Example See Also Send Feedback
Specifies the value to return.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns(
    TResult value
)

Parameters

value
Type: TResult
The value to return, or nullNothingnullptra null reference (Nothing in Visual Basic).

Examples

Return a true value from the method call:
CopyC#
mock.Setup(x => x.Execute("ping"))
    .Returns(true);

See Also

IReturns<(Of <(TMock, TResult>)>) Interface
Returns Overload
Moq.Language Namespace


IReturnsGetter(TMock, TProperty) Interface

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
Members See Also Send Feedback
Defines the Returns verb for property get setups.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IReturnsGetter<TMock, TProperty>
where TMock : class

Type Parameters

TMock
Mocked type.
TProperty
Type of the property.

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Members
Moq.Language Namespace


IReturnsGetter(TMock, TProperty) Members

[image]

Moq
IReturnsGetter<(Of <(TMock, TProperty>)>) Members
IReturnsGetter<(Of <(TMock, TProperty>)>) Interface Methods See Also Send Feedback

Methods

Name | Description
Returns | Overloaded.

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
Moq.Language Namespace


IReturnsGetter(TMock, TProperty) Methods

[image]

Moq
IReturnsGetter<(Of <(TMock, TProperty>)>) Methods
IReturnsGetter<(Of <(TMock, TProperty>)>) Interface See Also Send Feedback

Methods

Name | Description
Returns | Overloaded.

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
Moq.Language Namespace


Returns Method

[image]

Moq
IReturnsGetter<(Of <(TMock, TProperty>)>)..::.Returns Method
IReturnsGetter<(Of <(TMock, TProperty>)>) Interface See Also Send Feedback

Overload List

Name | Description
Returns(Func<(Of <(TProperty>)>)) | Specifies a function that will calculate the value to return for the property.
Returns(TProperty) | Specifies the value to return.

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
IReturnsGetter<(Of <(TMock, TProperty>)>) Members
Moq.Language Namespace


Returns Method ((TProperty))

IReturnsGetter<(Of <(TMock, TProperty>)>)..::.Returns Method (<(Of <(TProperty>)>))
IReturnsGetter<(Of <(TMock, TProperty>)>) Interface Example See Also Send Feedback
Specifies a function that will calculate the value to return for the property.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns(
    Func<TProperty> valueFunction
)

Parameters

valueFunction
Type: Func<(Of <( TProperty >)>)
The function that will calculate the return value.

Examples

Return a calculated value when the property is retrieved:
CopyC#
mock.SetupGet(x => x.Suspended)
    .Returns(() => returnValues[0]);
 The lambda expression to retrieve the return value is lazy-executed, meaning that its value may change depending on the moment the property is retrieved and the value the returnValues array has at that moment.

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
Returns Overload
Moq.Language Namespace


Returns Method (TProperty)

IReturnsGetter<(Of <(TMock, TProperty>)>)..::.Returns Method (TProperty)
IReturnsGetter<(Of <(TMock, TProperty>)>) Interface Example See Also Send Feedback
Specifies the value to return.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IReturnsResult<TMock> Returns(
    TProperty value
)

Parameters

value
Type: TProperty
The value to return, or nullNothingnullptra null reference (Nothing in Visual Basic).

Examples

Return a true value from the property getter call:
CopyC#
mock.SetupGet(x => x.Suspended)
    .Returns(true);

See Also

IReturnsGetter<(Of <(TMock, TProperty>)>) Interface
Returns Overload
Moq.Language Namespace


IThrows Interface

IThrows Interface
Members See Also Send Feedback
Defines the Throws verb.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IThrows

See Also

IThrows Members
Moq.Language Namespace


IThrows Members

[image]

Moq
IThrows Members
IThrows Interface Methods See Also Send Feedback

Methods

Name | Description
Throws | Overloaded.

See Also

IThrows Interface
Moq.Language Namespace


IThrows Methods

[image]

Moq
IThrows Methods
IThrows Interface See Also Send Feedback

Methods

Name | Description
Throws | Overloaded.

See Also

IThrows Interface
Moq.Language Namespace


Throws Method

[image]

Moq
IThrows..::.Throws Method
IThrows Interface See Also Send Feedback

Overload List

Name | Description
Throws<(Of <(TException>)>)()()() | Specifies the type of exception to throw when the method is invoked.
Throws(Exception) | Specifies the exception to throw when the method is invoked.

See Also

IThrows Interface
IThrows Members
Moq.Language Namespace


Throws(TException) Method

IThrows..::.Throws<(Of <(TException>)>) Method
IThrows Interface Example See Also Send Feedback
Specifies the type of exception to throw when the method is invoked.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IThrowsResult Throws<TException>()
where TException : new(), Exception

Type Parameters

TException
Type of exception to instantiate and throw when the setup is matched.

Examples

This example shows how to throw an exception when the method is invoked with an empty string argument:
CopyC#
mock.Setup(x => x.Execute(""))
    .Throws<ArgumentException>();

See Also

IThrows Interface
Throws Overload
Moq.Language Namespace


Throws Method ()

IThrows..::.Throws Method ()
IThrows Interface Example See Also Send Feedback
Specifies the exception to throw when the method is invoked.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
IThrowsResult Throws(
    Exception exception
)

Parameters

exception
Type: Exception
Exception instance to throw.

Examples

This example shows how to throw an exception when the method is invoked with an empty string argument:
CopyC#
mock.Setup(x => x.Execute(""))
    .Throws(new ArgumentException());

See Also

IThrows Interface
Throws Overload
Moq.Language Namespace


IVerifies Interface

IVerifies Interface
Members See Also Send Feedback
Defines the Verifiable verb.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IVerifies

See Also

IVerifies Members
Moq.Language Namespace


IVerifies Members

[image]

Moq
IVerifies Members
IVerifies Interface Methods See Also Send Feedback

Methods

Name | Description
Verifiable | Overloaded.

See Also

IVerifies Interface
Moq.Language Namespace


IVerifies Methods

[image]

Moq
IVerifies Methods
IVerifies Interface See Also Send Feedback

Methods

Name | Description
Verifiable | Overloaded.

See Also

IVerifies Interface
Moq.Language Namespace


Verifiable Method

[image]

Moq
IVerifies..::.Verifiable Method
IVerifies Interface See Also Send Feedback

Overload List

Name | Description
Verifiable()()() | Marks the expectation as verifiable, meaning that a call to Verify()()() will check if this particular expectation was met.
Verifiable(String) | Marks the expectation as verifiable, meaning that a call to Verify()()() will check if this particular expectation was met, and specifies a message for failures.

See Also

IVerifies Interface
IVerifies Members
Moq.Language Namespace


Verifiable Method

IVerifies..::.Verifiable Method
IVerifies Interface Example See Also Send Feedback
Marks the expectation as verifiable, meaning that a call to Verify()()() will check if this particular expectation was met.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
void Verifiable()

Examples

The following example marks the expectation as verifiable:
CopyC#
mock.Expect(x => x.Execute("ping"))
    .Returns(true)
    .Verifiable();

See Also

IVerifies Interface
Verifiable Overload
Moq.Language Namespace


Verifiable Method ()

IVerifies..::.Verifiable Method ()
IVerifies Interface Example See Also Send Feedback
Marks the expectation as verifiable, meaning that a call to Verify()()() will check if this particular expectation was met, and specifies a message for failures.

Namespace: Moq.Language
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
void Verifiable(
    string failMessage
)

Parameters

failMessage
Type: String

[Missing <param name="failMessage"/> documentation for "M:Moq.Language.IVerifies.Verifiable(System.String)"]

Examples

The following example marks the expectation as verifiable:
CopyC#
mock.Expect(x => x.Execute("ping"))
    .Returns(true)
    .Verifiable("Ping should be executed always!");

See Also

IVerifies Interface
Verifiable Overload
Moq.Language Namespace


Moq.Protected Namespace

Moq.Protected Namespace
Send Feedback
Importing this namespace will enable the Protected() method on mocks, enabling expectations on protected members by specifying the member name as a string.

Classes

Class | Description
ItExpr | Allows the specification of a matching condition for an argument in a protected member setup, rather than a specific argument value. "ItExpr" refers to the argument being matched.
ProtectedExtension | Enables the Protected() method on Mock<(Of <(T>)>), allowing setups to be set for protected members by using their name as a string, rather than strong-typing them which is not possible due to their visibility.

Interfaces

Interface | Description
IProtectedMock<(Of <(TMock>)>) | Allows setups to be specified for protected members by using their name as a string, rather than strong-typing them which is not possible due to their visibility.


IProtectedMock(TMock) Interface

IProtectedMock<(Of <(TMock>)>) Interface
Members See Also Send Feedback
Allows setups to be specified for protected members by using their name as a string, rather than strong-typing them which is not possible due to their visibility.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public interface IProtectedMock<TMock>
where TMock : class

Type Parameters

TMock

See Also

IProtectedMock<(Of <(TMock>)>) Members
Moq.Protected Namespace


IProtectedMock(TMock) Members

[image]

Moq
IProtectedMock<(Of <(TMock>)>) Members
IProtectedMock<(Of <(TMock>)>) Interface Methods See Also Send Feedback

The IProtectedMock<(Of <(TMock>)>) type exposes the following members.

Methods

Name | Description
Setup | Overloaded.
SetupGet<(Of <(TProperty>)>) | Specifies a setup for an invocation on a property getter with the given propertyName.
SetupSet<(Of <(TProperty>)>) | Specifies a setup for an invocation on a property setter with the given propertyName.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Moq.Protected Namespace


IProtectedMock(TMock) Methods

[image]

Moq
IProtectedMock<(Of <(TMock>)>) Methods
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback

The IProtectedMock<(Of <(TMock>)>) type exposes the following members.

Methods

Name | Description
Setup | Overloaded.
SetupGet<(Of <(TProperty>)>) | Specifies a setup for an invocation on a property getter with the given propertyName.
SetupSet<(Of <(TProperty>)>) | Specifies a setup for an invocation on a property setter with the given propertyName.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Moq.Protected Namespace


Setup Method

[image]

Moq
IProtectedMock<(Of <(TMock>)>)..::.Setup Method
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback

Overload List

Name | Description
Setup<(Of <(TResult>)>)(String, array<Object>[]()[]) | Specifies a setup for an invocation on a property or a non void method with the given methodOrPropertyName, optionally specifying arguments for the method call.
Setup(String, array<Object>[]()[]) | Specifies a setup for a void method invocation with the given voidMethodName, optionally specifying arguments for the method call.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
IProtectedMock<(Of <(TMock>)>) Members
Moq.Protected Namespace


Setup(TResult) Method (, [])

IProtectedMock<(Of <(TMock>)>)..::.Setup<(Of <(TResult>)>) Method (, array<>[]()[])
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback
Specifies a setup for an invocation on a property or a non void method with the given methodOrPropertyName, optionally specifying arguments for the method call.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ISetup<TMock, TResult> Setup<TResult>(
    string methodOrPropertyName,
    params Object[] args
)

Parameters

methodOrPropertyName
Type: String
Name of the method or property to be invoke.
args
Type: array< Object >[]()[]
Optional arguments for the invocation. If argument matchers are used, remember to use ItExpr rather than It.

Type Parameters

TResult
Return type of the method or property.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Setup Overload
Moq.Protected Namespace


Setup Method (, [])

IProtectedMock<(Of <(TMock>)>)..::.Setup Method (, array<>[]()[])
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback
Specifies a setup for a void method invocation with the given voidMethodName, optionally specifying arguments for the method call.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ISetup<TMock> Setup(
    string voidMethodName,
    params Object[] args
)

Parameters

voidMethodName
Type: String
Name of the void method to be invoke.
args
Type: array< Object >[]()[]
Optional arguments for the invocation. If argument matchers are used, remember to use ItExpr rather than It.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Setup Overload
Moq.Protected Namespace


SetupGet(TProperty) Method

IProtectedMock<(Of <(TMock>)>)..::.SetupGet<(Of <(TProperty>)>) Method
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback
Specifies a setup for an invocation on a property getter with the given propertyName.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ISetupGetter<TMock, TProperty> SetupGet<TProperty>(
    string propertyName
)

Parameters

propertyName
Type: String
Name of the property.

Type Parameters

TProperty
Type of the property.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Moq.Protected Namespace


SetupSet(TProperty) Method

IProtectedMock<(Of <(TMock>)>)..::.SetupSet<(Of <(TProperty>)>) Method
IProtectedMock<(Of <(TMock>)>) Interface See Also Send Feedback
Specifies a setup for an invocation on a property setter with the given propertyName.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
ISetupSetter<TMock, TProperty> SetupSet<TProperty>(
    string propertyName
)

Parameters

propertyName
Type: String
Name of the property.

Type Parameters

TProperty
Type of the property.

See Also

IProtectedMock<(Of <(TMock>)>) Interface
Moq.Protected Namespace


ItExpr Class

ItExpr Class
Members See Also Send Feedback
Allows the specification of a matching condition for an argument in a protected member setup, rather than a specific argument value. "ItExpr" refers to the argument being matched.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class ItExpr

Remarks

Use this variant of argument matching instead of It for protected setups.

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, or null.

Inheritance Hierarchy

Object
Moq.Protected..::.ItExpr

See Also

ItExpr Members
Moq.Protected Namespace


ItExpr Members

[image]

Moq
ItExpr Members
ItExpr Class Methods See Also Send Feedback

The ItExpr type exposes the following members.

Methods

Name | Description
Is<(Of <(TValue>)>) | Matches any value that satisfies the given predicate.
IsAny<(Of <(TValue>)>) | Matches any value of the given TValue type.
IsInRange<(Of <(TValue>)>) | Matches any value that is in the range specified.
IsNull<(Of <(TValue>)>) | Matches a null value of the given TValue type.
IsRegex | Overloaded.

See Also

ItExpr Class
Moq.Protected Namespace


ItExpr Methods

[image]

Moq
ItExpr Methods
ItExpr Class See Also Send Feedback

The ItExpr type exposes the following members.

Methods

Name | Description
Is<(Of <(TValue>)>) | Matches any value that satisfies the given predicate.
IsAny<(Of <(TValue>)>) | Matches any value of the given TValue type.
IsInRange<(Of <(TValue>)>) | Matches any value that is in the range specified.
IsNull<(Of <(TValue>)>) | Matches a null value of the given TValue type.
IsRegex | Overloaded.

See Also

ItExpr Class
Moq.Protected Namespace


Is(TValue) Method

ItExpr..::.Is<(Of <(TValue>)>) Method
ItExpr Class Example See Also Send Feedback
Matches any value that satisfies the given predicate.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression Is<TValue>(
    Expression<Predicate<TValue>> match
)

Parameters

match
Type: Expression<(Of <(Predicate<(Of <(TValue>)>)>)>)
The predicate used to match the method argument.

Type Parameters

TValue
Type of the argument to check.

Remarks

Allows the specification of a predicate to perform matching of method call arguments.

Examples

This example shows how to return the value 1 whenever the argument to the Do method is an even number.
CopyC#
mock.Protected()
    .Setup("Do", ItExpr.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:
CopyC#
mock.Protected()
    .Setup("GetUser", ItExpr.Is<int>(i => i < 0))
    .Throws(new ArgumentException());

See Also

ItExpr Class
Moq.Protected Namespace


IsAny(TValue) Method

ItExpr..::.IsAny<(Of <(TValue>)>) Method
ItExpr Class Example See Also Send Feedback
Matches any value of the given TValue type.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression IsAny<TValue>()

Type Parameters

TValue
Type of the value.

Remarks

Typically used when the actual argument value for a method call is not relevant.

Examples

CopyC#
// Throws an exception for a call to Remove with any string value.
mock.Protected()
    .Setup("Remove", ItExpr.IsAny<string>())
    .Throws(new InvalidOperationException());

See Also

ItExpr Class
Moq.Protected Namespace


IsInRange(TValue) Method

ItExpr..::.IsInRange<(Of <(TValue>)>) Method
ItExpr Class Example See Also Send Feedback
Matches any value that is in the range specified.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression IsInRange<TValue>(
    TValue from,
    TValue to,
    Range rangeKind
)
where TValue : IComparable

Parameters

from
Type: TValue
The lower bound of the range.
to
Type: TValue
The upper bound of the range.
rangeKind
Type: Moq..::.Range
The kind of range. See Range.

Type Parameters

TValue
Type 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.
CopyC#
mock.Protected()
    .Setup("HasInventory",
            ItExpr.IsAny<string>(),
            ItExpr.IsInRange(0, 100, Range.Inclusive))
    .Returns(false);

See Also

ItExpr Class
Moq.Protected Namespace


IsNull(TValue) Method

ItExpr..::.IsNull<(Of <(TValue>)>) Method
ItExpr Class Example See Also Send Feedback
Matches a null value of the given TValue type.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression IsNull<TValue>()

Type Parameters

TValue
Type of the value.

Remarks

Required for protected mocks as the null value cannot be used directly as it prevents proper method overload selection.

Examples

CopyC#
// Throws an exception for a call to Remove with a null string value.
mock.Protected()
    .Setup("Remove", ItExpr.IsNull<string>())
    .Throws(new InvalidOperationException());

See Also

ItExpr Class
Moq.Protected Namespace


IsRegex Method

[image]

Moq
ItExpr..::.IsRegex Method
ItExpr Class See Also Send Feedback

Overload List

Name | Description
IsRegex(String) | Matches a string argument if it matches the given regular expression pattern.
IsRegex(String, RegexOptions) | Matches a string argument if it matches the given regular expression pattern.

See Also

ItExpr Class
ItExpr Members
Moq.Protected Namespace


IsRegex Method ()

ItExpr..::.IsRegex Method ()
ItExpr Class Example See Also Send Feedback
Matches a string argument if it matches the given regular expression pattern.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression IsRegex(
    string regex
)

Parameters

regex
Type: String
The pattern to use to match the string argument value.

Examples

The following example shows how to expect a call to a method where the string argument matches the given regular expression:
CopyC#
mock.Protected()
    .Setup("Check", ItExpr.IsRegex("[a-z]+"))
    .Returns(1);

See Also

ItExpr Class
IsRegex Overload
Moq.Protected Namespace


IsRegex Method (, )

ItExpr..::.IsRegex Method (, )
ItExpr Class Example See Also Send Feedback
Matches a string argument if it matches the given regular expression pattern.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static Expression IsRegex(
    string regex,
    RegexOptions options
)

Parameters

regex
Type: String
The pattern to use to match the string argument value.
options
Type: RegexOptions
The options used to interpret the pattern.

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:
CopyC#
mock.Protected()
    .Setup("Check", ItExpr.IsRegex("[a-z]+", RegexOptions.IgnoreCase))
    .Returns(1);

See Also

ItExpr Class
IsRegex Overload
Moq.Protected Namespace


ProtectedExtension Class

ProtectedExtension Class
Members See Also Send Feedback
Enables the Protected() method on Mock<(Of <(T>)>), allowing setups to be set for protected members by using their name as a string, rather than strong-typing them which is not possible due to their visibility.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static class ProtectedExtension

Inheritance Hierarchy

Object
Moq.Protected..::.ProtectedExtension

See Also

ProtectedExtension Members
Moq.Protected Namespace


ProtectedExtension Members

[image]

Moq
ProtectedExtension Members
ProtectedExtension Class Methods See Also Send Feedback

The ProtectedExtension type exposes the following members.

Methods

Name | Description
Protected<(Of <(T>)>) | Enable protected setups for the mock.

See Also

ProtectedExtension Class
Moq.Protected Namespace


ProtectedExtension Methods

[image]

Moq
ProtectedExtension Methods
ProtectedExtension Class See Also Send Feedback

The ProtectedExtension type exposes the following members.

Methods

Name | Description
Protected<(Of <(T>)>) | Enable protected setups for the mock.

See Also

ProtectedExtension Class
Moq.Protected Namespace


Protected(T) Method

ProtectedExtension..::.Protected<(Of <(T>)>) Method
ProtectedExtension Class See Also Send Feedback
Enable protected setups for the mock.

Namespace: Moq.Protected
Assembly: Moq (in Moq.dll) Version: 4.0.812.4 (4.0.0.0)

Syntax

C#
public static IProtectedMock<T> Protected<T>(
    Mock<T> mock
)
where T : class

Parameters

mock
Type: Moq..::.Mock <(Of <(T>)>)
The mock to set the protected setups on.

Type Parameters

T
Mocked object type. Typically omitted as it can be inferred from the mock instance.

See Also

ProtectedExtension Class
Moq.Protected Namespace