gpt4 book ai didi

c# - Moq:从通用接口(interface)继承的模拟接口(interface)

转载 作者:行者123 更新时间:2023-11-30 15:21:44 25 4
gpt4 key购买 nike

我有一个定义如下的接口(interface):

public interface IBaseRepository<T>
{
IQueryable<T> All();
}

然后,我有一个扩展它的接口(interface):

public interface IAccountRepository : IBaseRepository<AccountModel>
{
}

然而,在我的测试中,当我尝试模拟 IAccountRepository 并为 IAccountRepository.All() 调用设置时,Moq 不允许我使用 Returns 方法来定义一个结果。

var mockAccountRepository = new Mock<IAccountRepository>();
mockAccountRepository.Setup(x => x.All()).Returns(...); // "Returns" can't work here for some reason.

如何在继承自泛型接口(interface)的接口(interface)上模拟基方法?

最佳答案

mockAccountRepository.Setup(x => x.All()).Returns(new List<AccountModel>().AsQueryable());

关于c# - Moq:从通用接口(interface)继承的模拟接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com