加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 模拟任务>与NSubstitute

发布时间:2020-12-16 03:29:54 所属栏目:asp.Net 来源:网络整理
导读:我在尝试让NSubstitute从Task返回IEnumerable接口时遇到问题. 工厂我在嘲笑: public interface IWebApiFactoryT : IDisposable{ T GetOne(int id); TaskIEnumerableT GetAll(); TaskIEnumerableT GetMany(); void SetAuth(string token);} 测试方法: [Test
我在尝试让NSubstitute从Task返回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>>

这是一个问题,我传递一个列表,作为一个提出IEnumerable,即使List是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);

解决方法

没有任何重载是您传递的值的良好匹配.第二个签名,public static ConfiguredCall返回< T>(此T值,params T [] returnThese);期望与函数的返回类型相同类型的值,因此它不是最佳匹配.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读