How to stub out a generic method definition in an interface using Microsoft Fakes in c#

北城余情 提交于 2019-12-14 02:36:27

问题


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

The stub looks like this:

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.

Any ideas?


回答1:


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


来源:https://stackoverflow.com/questions/32307972/how-to-stub-out-a-generic-method-definition-in-an-interface-using-microsoft-fake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!