【问题标题】:MOQ stubbing property value on "Any" object“任何”对象上的 MOQ 存根属性值
【发布时间】:2013-06-02 11:41:30
【问题描述】:

我正在编写一些代码,该代码遵循将方法的所有参数封装为“请求”对象并返回“响应”对象的模式。但是,这在使用 MOQ 进行模拟时产生了一些问题。例如:

public class Query : IQuery
{
    public QueryResponse Execute(QueryRequest request)
    {
        // get the customer...
        return new QueryResponse { Customer = customer };
    }
}

public class QueryRequest
{
    public string Key { get; set; }
}

public class QueryResponse
{
    public Customer Customer { get; set; }
}

...在我的测试中,我想存根查询以在给定密钥时返回客户

var customer = new Customer();
var key = "something";
var query = new Mock<ICustomerQuery>();

// I want to do something like this (but this does not work)
// i.e. I dont care what the request object that get passed is in but it must have the key value I want to give it

query.Setup(q => q.Execute(It.IsAny<QueryRequest>().Key = key))
     .Returns(new QueryResponse {Customer = customer});

最小起订量可以实现我想要的吗?

【问题讨论】:

    标签: c# mocking moq stubbing moq-3


    【解决方案1】:

    我怀疑您可以使用自定义匹配器来做到这一点。

    来自moq's QuickStart page

    // custom matchers
    mock.Setup(foo => foo.Submit(IsLarge())).Throws<ArgumentException>();
    ...
    public string IsLarge() 
    { 
      return Match.Create<string>(s => !String.IsNullOrEmpty(s) && s.Length > 100);
    }
    

    我怀疑你可以做类似的事情。创建一个使用Match.Create&lt;QueryRequest&gt; 匹配您的密钥的方法,例如

    public QueryRequest CorrectKey(string key)
    {
        return Match.Create<QueryRequest>(qr => qr.Key == key);
    }
    

    然后

    _query.Setup(q => q.Execute(CorrectKey(key))).Returns(new QueryResponse {Customer = customer});
    

    注意:我没有尝试过这段代码,如果它完全中断,请原谅我。

    哦,还有一些稍微相关的自我宣传:正是这种复杂性让我对 Moq 和其他模拟工具感到困惑。这就是为什么我创建了一个模拟库,允许您使用普通代码检查方法参数:http://github.com/eteeselink/FakeThat。不过,它正处于重大重构(和重命名)过程的中间,因此您可能需要屏住呼吸。不过,我很高兴听到您对此的看法。

    编辑:哦,@nemesv 以(可能)更好的答案击败了我。嗯嗯。

    【讨论】:

      【解决方案2】:

      您正在寻找的是It.Is&lt;T&gt; 方法,您可以在其中为参数指定任何匹配器函数(Func&lt;T, bool&gt;)。

      例如检查密钥:

      query.Setup(q => q.Execute(It.Is<QueryRequest>(q => q.Key == key)))
           .Returns(new QueryResponse {Customer = customer});
      

      【讨论】:

        猜你喜欢
        • 2019-03-09
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 2017-01-15
        • 2015-05-30
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多