Stub(T, TProperty) Method (Mock(T), ((T, TProperty)))

Moq 2.6

Specifies that the given property should have stub behavior, meaning that setting its value will cause it to be saved and later returned when the property is requested.

Namespace:  Moq.Stub
Assembly:  Moq (in Moq.dll) Version: 2.6.1014.1 (2.6.0.0)

Syntax

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

Parameters

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

Type Parameters

T
Mocked type, inferred from the object where this method is being applied (does not need to be specified).
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