I am trying to verify a parameter that is a class. The code being tested is fine. The bug is in the test.
I have tried two methods, both of which have failed.
Here are my attempts:
1:
this.MockImageResizeFilter.Verify(m => m.Filter(this.UploadedFileData, new ImageFilterOptions()
{
Width = 256,
Height = 256,
}));
This always fails, even though an object passed as the second parameter has equal properties. The first parameter is verified fine.
2:
this.MockImageResizeFilter.Setup(m => m.Filter(It.IsAny<byte[]>(), It.IsAny<ImageFilterOptions>()))
.Callback<byte[], ImageFilterOptions>((data, options) =>
{
Assert.AreEqual(this.UploadedFileData, data, "data");
Assert.AreEqual(filterOptions.Width, options.Width, "Width");
Assert.AreEqual(filterOptions.Height, options.Height, "Height");
}
);
This always passes, even when it should fail. The Asserts in the callback do fail, but the exception is not passed to the outer context, and thus the test always passes.
Can you help me find what I am doing wrong?
The first attempt is closer to what you want to achieve.
The reason it fails is that Moq (probably) uses
Object.Equalsunder the cover to test if theImageFilterOptionsparameter that the method was called with is the same instance as the one you supplied in the call toVerify.It is impossible for them to be the same instance, because in
Verifyyou create anew ImageFilterOptions().Instead of performing the parameter check this way, you could use Moq’s
It.Issyntax to provide an expression that verifies the parameter was the expected one.In your case, you want to check that the parameter is of type
ImageFilterOptionsand that both theWidthand theHeightare set to256. The expression that allows you to do that is:So, your call to
Verifycould look like this: