如何使用Microsoft Fakes in c#在接口中存根通用方法定义

时间:2015-08-31 09:27:49

标签: unit-testing generics mocking microsoft-fakes stubbing

我有一个单元测试,它使用Microsoft Fakes来截取以下界面:

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

存根看起来像这样:

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

问题是我如何将这个新版本的接口存根?我无法正确使用语法。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以按以下方式设置行为:

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

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