I'm injecting a dependency CheckCompatibRepository
. I'm mocking a method IsCompatible
which has a list as 3rd parameter.
var mockRepositoryCheckCompatib = new Mock<ICheckCompatibilityActDoer>();
mockRepositoryCheckCompatib.Setup(c => c.IsCompatible(doer, activity, listActivitiesPreDispatched)).Returns(true);
The problem is the list. It's populated by the class I'm testing. Honnestly, I don't care about that parameter, I want to mock IsCompatible
ignoring that parameter. Is that possible?
Otherwise, the mocking just won't catch the method calls. To ease things, I tried by sending the list as an injected dependency in my class. It works until the list starts being populated, then the mock stops catching the calls.
How would you mock this?
Since you're using Moq, you're looking for the It.IsAny<T>
method. Since you're changing the values of the list, passing it an object instance is not the right way to go as you would have to continually setup the Mock to handle the new parameter.
The following example will allow the mock to accept any parameter value of type List<T>
. For the sake of this example, we will use List<int>
.
var mockRepositoryCheckCompatib = new Mock<ICheckCompatibilityActDoer>();
mockRepositoryCheckCompatib.Setup(c => c.IsCompatible(doer, activity, It.IsAny<List<int>>())).Returns(true);
Edit: I didn't see bzlm's comment from earlier, which essentially answers the question. Please accept his answer if he posts one, I didn't mean to poach it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With