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