c# – “任意”对象上的MOQ存根属性值

c# – “任意”对象上的MOQ存根属性值,第1张

概述我正在研究一些代码,这些代码遵循将方法的所有参数封装为“请求”对象并返回“响应”对象的模式.但是,在使用MOQ进行模拟时,这会产生一些问题.例如: public class Query : IQuery{ public QueryResponse Execute(QueryRequest request) { // get the customer... 我正在研究一些代码,这些代码遵循将方法的所有参数封装为“请求”对象并返回“响应”对象的模式.但是,在使用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 itquery.Setup(q => q.Execute(It.IsAny<queryRequest>().Key = key)).Returns(new queryResponse {Customer = customer});

MOQ中我想要的是什么?

解决方法 你正在寻找它It.Is< T>方法,您可以为参数指定任何匹配器函数(Func< T,bool>).

例如检查密钥:

query.Setup(q => q.Execute(It.Is<queryRequest>(q => q.Key == key)))     .Returns(new queryResponse {Customer = customer});
总结

以上是内存溢出为你收集整理的c# – “任意”对象上的MOQ存根属性值全部内容,希望文章能够帮你解决c# – “任意”对象上的MOQ存根属性值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249939.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存