Moq (It.isAny & mock multiple calls)
Mocking examples with Moq
It.IsAny<T> variables
Those are argument matchers we have in Moq for the case we don’t have the same instance match.
Exact instance match - most restrictive
Example for default comparison
[Theory, Autodata]
public async Task FilledString_Execute_CorrectlyProcessed(string name, AnimalDTO animal)
{
// ARRANGE
_dependencyMock
.Setup(m => m.Load(name))
.Returns(animal)
// ACT
AnimalDTO result = await _sut.Execute(name);
// ASSERT
_dependencyMock.Verify(m => m.Load(name));
}
It.IsAny<T> - most permissive
Matches any instance of that type, null included.
[Theory, Autodata]
public async Task FilledString_Execute_CorrectlyProcessed(string name, AnimalDTO animal)
{
// ARRANGE
_dependencyMock
.Setup(m => m.Load(name))
.Returns(animal)
// ACT
AnimalDTO result = await _sut.Execute(name);
// ASSERT
// we use this in case the property is not accesible
// for example: a param created inside the class
_dependencyMock.Verify(m => m.Load(It.IsAny<string>()));
}