使用Moq,您可以验证匿名类型的方法调用吗? [英] Using Moq can you verify a method call with an anonymous type?

查看:59
本文介绍了使用Moq,您可以验证匿名类型的方法调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Moq验证方法调用,但是我不太正确地使用了语法.目前,我已将其作为验证:

I'm trying to verify a method call using Moq, but I can't quite get the syntax right. Currently I've go this as my verify:

repository.Verify(x => x.ExecuteNonQuery("fav_AddFavorites", new
{
    fid = 123,
    inputStr = "000456"
}), Times.Once());

代码可以编译,但是测试失败并显示以下错误:

The code compiles, but the test fails with the error:

Expected invocation on the mock once, but was 0 times: 
x => x.ExecuteNonQuery("fav_AddFavorites", new <>f__AnonymousType0<Int32, String>(123, "000456"))
No setups configured.

Performed invocations:
IRepository.ExecuteNonQuery("fav_AddFavorites", { fid = 123, inputStr = 000456 })

如何验证方法调用并匹配匿名类型的方法参数.

How can I verify the method call and match the method parameters for an anonymous type.

更新

回答问题:

我正在尝试验证方法是否已调用以及参数是否正确.

I am trying to verify both that the method was called and that the parameters are correct.

我要验证的方法的签名是:

The signature of the method I'm trying to verify is:

int ExecuteNonQuery(string query, object param = null);

设置代码很简单:

repository = new Mock<IRepository>();

更新2

这似乎是Moq的问题,以及它如何处理.Net中的匿名类型. Paul Matovich发布的代码运行良好,但是,一旦代码和测试位于不同的程序集中,则测试将失败.

It looks like this is a problem with Moq and how it handles anonymous types in .Net. The code posted by Paul Matovich runs fine, however, once the code and the test are in difference assemblies the test fails.

推荐答案

通过

        public class Class1
    {
        private Class2 _Class2;
        public Class1(Class2 class2)
        {
            _Class2 = class2;
        }

        public void DoSomething(string s)
        {
            _Class2.ExecuteNonQuery(s, new { fid = 123,  inputStr = "000456" });
        }
    }

    public class Class2
    {
        public virtual void ExecuteNonQuery(string s, object o)
        {
        }
    }

    /// <summary>
    ///A test for ExecuteNonQuery
    ///</summary>
    [TestMethod()]
    public void ExecuteNonQueryTest()
    {
        string testString = "Hello";
        var Class2Stub = new Mock<Class2>();
        Class1 target = new Class1(Class2Stub.Object);
        target.DoSomething(testString);
        Class2Stub.Verify(x => x.ExecuteNonQuery(testString, It.Is<object>(o => o.Equals(new { fid = 123, inputStr = "000456" }))), Times.Once());
    }

更新

这很奇怪,它不能在不同的程序集中工作.有人可以给我们一个长定义,说明为什么来自不同程序集的object.equals表现出不同的行为,但是对于不同的程序集,这将起作用,对象值的任何差异都将返回不同的哈希码.

Update

That is strange, it doesn't work in different assemblies. Someone can give us the long definition about why the object.equals from different assemblies behaves differently, but for different assemblies this will work, any variance in the object values will return a different hash code.

        Class2Stub.Verify(x => x.ExecuteNonQuery(testString, It.Is<object>(o => o.GetHashCode() == (new { fid = 123, inputStr = "000456" }).GetHashCode())), Times.Once());

这篇关于使用Moq,您可以验证匿名类型的方法调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆