Moq:从通用接口继承的模拟接口
Moq: Mock interface that inherits from generic interface
我有一个接口定义如下:
public interface IBaseRepository<T>
{
IQueryable<T> All();
}
然后,我有一个扩展这个的接口:
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.
如何在继承自泛型接口的接口上模拟基方法?
mockAccountRepository.Setup(x => x.All()).Returns(new List<AccountModel>().AsQueryable());
我有一个接口定义如下:
public interface IBaseRepository<T>
{
IQueryable<T> All();
}
然后,我有一个扩展这个的接口:
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.
如何在继承自泛型接口的接口上模拟基方法?
mockAccountRepository.Setup(x => x.All()).Returns(new List<AccountModel>().AsQueryable());