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