使用 NSubstitute 模拟任务<IEnumerable<T>>

Mocking Task<IEnumerable<T>> with NSubstitute

我在尝试从 Task.

获取 NSubstitute 到 return 一个 IEnumerable 接口时遇到问题

我嘲笑的工厂:

public interface IWebApiFactory<T> : IDisposable
{
    <T> GetOne(int id);
    Task<IEnumerable<T>> GetAll();
    Task<IEnumerable<T>> GetMany();
    void SetAuth(string token);
}

测试方法:

[TestMethod]
public async Task TestMutlipleUsersAsViewResult()
{
    var employees = new List<EmployeeDTO>()
    {
        new EmployeeDTO(),
        new EmployeeDTO()
    };

    // Arrange
    var factory = Substitute.For<IWebApiFactory<EmployeeDTO>>();
    factory.GetMany().Returns(Task.FromResult(employees));
}

我得到的错误是:

cannot convert from 'System.Threading.Tasks.Task> to System.Func>>

即使 ListIEnumerable,我传递一个列表作为对 IEnumerable 的一个问题是否存在?

编辑:

这些是NSubstitute

中的函数
public static ConfiguredCall Returns<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese);
public static ConfiguredCall Returns<T>(this T value, T returnThis, params T[] returnThese);

None 的重载与您传递的值非常匹配。第二个签名 public static ConfiguredCall Returns<T>(this T value, T returnThis, params T[] returnThese); 需要一个与函数的 return 类型相同类型的值,因此它不是最佳匹配。

解决这个问题的最简单方法是将员工声明更改为 IEnumerable<EmployeeDTO> :

IEnumerable<EmployeeDTO> employees = new List<EmployeeDTO>()
{
    new EmployeeDTO(),
    new EmployeeDTO()
};