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

Moq 2.6

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: 2.6.1014.1 (2.6.0.0)

Syntax

C#
IReturnsResult 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 expectation 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.Expect(x => x.Execute(It.IsAny<string>()))
    .Returns((string command) => returnValues[command]);

See Also