c# - Moq - 模拟多个参数

标签 c# mocking moq

我在 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/

相关文章:

c# - 从 List<ItemDetails> 是实体的类创建 XML

ios - OCMock 测试分配对象并调用方法

C# 如何测试对接口(interface)的引用

c# - 如何让 AutoFixture AutoMoq 从实例化对象中的注入(inject)服务返回结果?

c# - 当等待模拟方法时,单元测试中的 NRE

c# - GUID 的哪一部分最值得保留?

c# - 将 DataColumn.DataType 转换为 SqlDbType

c# - 如何在 C# 中比较 2 个 DateTime

sharepoint - 在 SharePoint 中对事件处理程序进行单元测试?

java - DI 的部分模拟?