C# Moq 使用对象参数验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13495801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:46:26  来源:igfitidea点击:

Moq verify with object parameter

c#.netunit-testingmoq

提问by rhughes

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:

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:

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.

这总是通过,即使它应该失败。回调中的 Asserts 确实失败,但异常不会传递到外部上下文,因此测试总是通过。

Can you help me find what I am doing wrong?

你能帮我找出我做错了什么吗?

采纳答案by Cristian Lupascu

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 the ImageFilterOptionsparameter that the method was called with is the same instance as the one you supplied in the call to Verify.

它失败的原因是 Moq(可能)Object.Equals在幕后使用来测试ImageFilterOptions调用该方法的 参数是否与您在调用Verify.

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

它们不可能是同一个实例,因为在Verify您创建一个new 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.

您可以使用 Moq 的It.Is语法来提供验证参数是否为预期参数的表达式,而不是以这种方式执行参数检查。

In your case, you want to check that the parameter is of type ImageFilterOptionsand that both the Widthand the Heightare set to 256. The expression that allows you to do that is:

在您的情况下,您要检查参数是否为 typeImageFilterOptions并且 theWidth和 theHeight都设置为256。允许您这样做的表达式是:

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

So, your call to Verifycould look like this:

因此,您的调用Verify可能如下所示:

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