最小起订量-模拟多个参数

Ter*_*rry 4 c# moq mocking

我在Moq页面上尝试的链接有一半是断开的,包括用于其官方API文档的链接。所以我会在这里问。

我已经成功地使用了一个“ catch all”参数,如下所示:

mockRepo.Setup(r => r.GetById(It.IsAny<int>())).Returns((int i) => mockCollection.Where(x => x.Id == i).Single());
Run Code Online (Sandbox Code Playgroud)

但是我无法弄清楚如何使用多个参数实现相同的行为。

mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Returns( ..... );
Run Code Online (Sandbox Code Playgroud)

....是我不知道的部分。


编辑以回应约旦:

问题是如何表示3个参数而不是仅3个。

如何转弯:

(int i) => mockCollection.Where(x => x.Id == i)
Run Code Online (Sandbox Code Playgroud)

变成:

(int i), (string s), (int j) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == j)
Run Code Online (Sandbox Code Playgroud)

Erw*_*win 5

它与单个参数几乎相同:

.Returns((int i, string s, int x) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));
Run Code Online (Sandbox Code Playgroud)

或使用return的通用变体:

.Returns<int, string, int>((i, s, x) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));
Run Code Online (Sandbox Code Playgroud)


Mik*_*ill 5

我认为您需要的是:

   mockRepo
       .Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>()))
       .Returns<int,string,int>((id, someProp, someOtherProp) =>
           mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));
Run Code Online (Sandbox Code Playgroud)