如何在使用Microsoft的界面中存储通用方法定义 [英] How to stub out a generic method definition in an interface using Microsoft Fakes in c#

查看:164
本文介绍了如何在使用Microsoft的界面中存储通用方法定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试,它使用Microsoft Fakes存根下面的接口:

I have a unit test which stubs out the following interface using 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);
                  }
            };

这很好。不过,我想将接口更改为更通用的方式,如下所示:

This works fine. However I'd like to change the interface to be more generic like so:

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

问题是如何将这个新版本的接口存根出来?

Question is how would I stub this new version of the interface out? I'm having trouble getting the syntax right.

有什么想法?

推荐答案

您将行为设置为以下内容:

You set the behavior as the following:

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

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

这篇关于如何在使用Microsoft的界面中存储通用方法定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆