Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Moq.Mock.Verify() compare parameters using identity or .Equals()?

Tags:

c#

moq

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?

like image 878
Kevin Avatar asked Jan 28 '13 00:01

Kevin


1 Answers

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))));
like image 113
Bassam Mehanni Avatar answered Oct 14 '22 01:10

Bassam Mehanni