Moq verify with object parameter

rhughes picture rhughes · Nov 21, 2012 · Viewed 29.5k times · Source

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?

Answer

Cristian Lupascu picture Cristian Lupascu · Nov 21, 2012

The first attempt is closer to what you want to achieve.

The reason it fails is that Moq (probably) uses Object.Equals under the cover to test if the ImageFilterOptions parameter that the method was called with is the same instance as the one you supplied in the call to Verify.

It is impossible for them to be the same instance, because in Verify you create a new ImageFilterOptions().

Instead of performing the parameter check this way, you could use Moq's It.Is syntax 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 ImageFilterOptions and that both the Width and the Height are set to 256. The expression that allows you to do that is:

It.Is<ImageFilterOptions>(p => p.Width == 256 && p.Height == 256)

So, your call to Verify could look like this:

this.MockImageResizeFilter.Verify(m => m.Filter(
            this.UploadedFileData,
            It.Is<ImageFilterOptions>(p => p.Width == 256 && p.Height == 256)));