Results
When configuring mocks, it is important to specify the return value of functions (methods that return a value) and properties.
Moq supports this scenario with the Returns
construct.
As previously seen, the most elementary scenario is configuring a method or a property to return a well-known value, i.e. a value already available while configuring the mock.
Note that the return value must match with the return type of the the configured member.
Asynchronous functions
Moq exposes helper methods to deal with asynchronous functions (i.e. methods returning Task).
In the snippet below, the two lines are interchangeable, with the clear advantage of letting Moq handling the Task API.
Computed return values
While configuring the mock, there might be the need for computing the return value based on the incoming inputs. To handle this scenario, Returns
and ReturnsAsync
have a set of overloads accepting a delegate instead of a finite value.
Note that the delegate must match the method signature and will be lazily invoked when the method is invoked.
Returning null
null
Due to how overload resolution works in C#, configuring a method or a property to return a null
value requires some attention.
In fact, simply typing .Returns(null)
will cause a compiler error:
CS0121 The call is ambiguous between the following methods or properties: 'IReturns.Returns(TResult)' and 'IReturns.Returns(Func)'
To solve this, it's enough to cast null
to the target type.
Last updated