我在 Moq 页面尝试的链接有一半都坏了,包括 their official API documentation 的链接。 .所以我会在这里问。
我已经成功地使用了一个“catch all”参数,如下所示:
mockRepo.Setup(r => r.GetById(It.IsAny<int>())).Returns((int i) => mockCollection.Where(x => x.Id == i).Single());
但是我不知道如何使用多个参数实现相同的行为。
mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Returns( ..... );
....
是我想不通的部分。
编辑以回应 Jordan:
问题是如何表示 3 个参数而不是一个。
如何转向:
(int i) => mockCollection.Where(x => x.Id == i)
进入:
(int i), (string s), (int j) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == j)
最佳答案
这与使用单个参数几乎相同:
.Returns
(
(int i, string s, int x)
=> mockCollection.Where
(
x => x.Id == i
&& x.SomeProp == s
&& x.SomeOtherProp == x
)
);
或者使用返回的通用变体:
.Returns<int, string, int>
(
(i, s, x)
=> mockCollection.Where
(
x => x.Id == i
&& x.SomeProp == s
&& x.SomeOtherProp == x
)
);
关于c# - Moq - 模拟多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679599/