gpt4 book ai didi

c# - "Any"对象上的 MOQ stub 属性值

转载 作者:太空狗 更新时间:2023-10-29 20:05:16 25 4
gpt4 key购买 nike

我正在编写一些代码,这些代码遵循将方法的所有参数封装为“请求”对象并返回“响应”对象的模式。然而,这在使用 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; }
}

...在我的测试中,我想 stub 查询以在给定 key 时返回客户

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});

我想要的最小起订量是否可行?

最佳答案

你在找什么It.Is<T>方法,您可以在其中为参数指定任何匹配器函数(Func<T, bool>)。

例如检查 key :

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

关于c# - "Any"对象上的 MOQ stub 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com