Setupmethod, developers can configure method calls on mocks by leveraging the different argument match techniques shown earlier.
Setupalso supports asynchronous methods.
Returnswill be explained more in details later).
refkeyword when declaring your method's arguments. Here is a page on the official documentation regarding passing reference-type parameters by reference.
DoSomethingis invoked, the variable holding the reference to the passed argument will be holding a reference to the
Funcdelegates with ref parameters: to obviate this issue, we define a custom delegate and we use it to create a callback function that Moq will invoke when the method is invoked. We will look at how Moq supports callbacks later.
DoSomethingis invoked, the out parameter will be a reference to the preset variable: in the example, the variable
CancellationTokento be provided to most asynchronous methods. As most libraries specify a default value for the
CancellationTokenargument, developers often overlook the necessity to specify a value for that argument. In this case developers can either use the
IsAnyconstruct or the
CancellationTokenwhile in the latter, we're specifying a specific value. If the component under test were not to rely on the default value, Moq would not capture the invocation.