







![]() |
Moq |
StubExtensions..::.Stub<(Of <(T, TProperty>)>) Method (Mock<(Of <(T>)>), <(Of <(<(Of <(T, TProperty>)>)>)>), TProperty) |
StubExtensions Class Example See Also Send Feedback |
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. This overload
allows setting the initial value for the property.
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, TProperty initialValue ) 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.
- initialValue
- Type: TProperty
Initial value for the property.
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#
After the Stub call has been issued, setting and
retrieving the object value will behave as expected:
CopyC#

var mock = new Mock<IHaveValue>(); mock.Stub(v => v.Value, 5);

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);