unit-testing - 如何在 C# 中使用 Microsoft Fakes 在接口(interface)中 stub 通用方法定义

标签 unit-testing generics mocking microsoft-fakes stubbing

我有一个单元测试,它使用 Microsoft Fakes 将以下接口(interface) stub :

public interface ITable
{
    Task<TableResult> Retrieve(string tableReference, string partitionKey, string rowKey);
}

stub 看起来像这样:
ITable table = new MessagesAPI.Azure.Fakes.StubITable()
            {
                RetrieveStringStringString = delegate
                  {
                      TableResult tableResult = new TableResult();
                      return Task.FromResult(tableResult);
                  }
            };

这工作正常。但是我想将界面更改为更通用,如下所示:
public interface ITable
{
    Task<TableResult> Retrieve<T>(string tableReference, string partitionKey, string rowKey) 
                     where T : ITableEntity;
}

问题是我将如何 stub 这个新版本的界面?我无法正确使用语法。

有任何想法吗?

最佳答案

您将行为设置如下:

var table = new MessagesAPI.Azure.Fakes.StubITable();

table.RetrieveOf1StringStringString<ITableEntity>(
     (tableReference, partitionKey, rowKey) =>
{
    TableResult tableResult = new TableResult();
    return Task.FromResult(tableResult);
});

关于unit-testing - 如何在 C# 中使用 Microsoft Fakes 在接口(interface)中 stub 通用方法定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307972/

相关文章:

node.js - 单元测试中请求 NPM 模块的 stub 响应,以便测试 pipeline()

c# - 类型 'T' 必须是不可为 null 的值类型才能将其用作泛型类型或方法 'T' 中的参数 'System.Nullable<T>'

java - BinarySearchingTree - dll(generic),不知何故我的项目无法添加到我的 adt 中

unit-testing - Mocking : Unit test question 中的依赖项太多

iphone - OCUnit 和 OCMock 在 iPhone SDK 上工作吗?

c# - 如何对 FileStream 的 File.Open 进行单元测试

javascript - AngularJS - 指令单元测试什么也没有

node.js - 找不到名称 "module"

Java 泛型异常会产生编译时错误

php - 在 PHPUnit 模拟对象中返回不同的结果