In a command like
var mockObj = new Mock<MyObject>()
var anotherObj = Utilities.DoStuff();
// some tests...
mockObj.Verify(foo => foo.someMethod(anotherObj));
Does Moq use comparison by identity or by using .Equals()
to determine whether someMethod()
was ever called with anotherObj
as a parameter? In other words, does the object I indicate as a parameter for foo.someMethod()
have to be the exact same object that someMethod()
was called with earlier for the verification to pass, or does it only have to be one that is equal to anotherObj
?
Moq
will compare by identity, it will be looking for the exact instance that you specified using identity. If this is not what you want, and you are looking for equals
comparison instead, you can use It.Is
:
mockObj.Verify(foo => foo.someMethod(It.Is<MyObject>(m => m.Equals(anotherObj))));
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