简体   繁体   English

Moq:继承自通用接口的Mock接口

[英]Moq: Mock interface that inherits from generic interface

I have an interface defined as follows: 我有一个接口定义如下:

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

Then, I have an interface extending this: 然后,我有一个扩展这个的接口:

public interface IAccountRepository : IBaseRepository<AccountModel>
{
}

In my tests, however, when I try to mock the IAccountRepository and call setup for IAccountRepository.All() , Moq won't allow me to use the Returns method to define a result. 但是,在我的测试中,当我尝试模拟IAccountRepository并调用IAccountRepository.All()设置时,Moq将不允许我使用Returns方法来定义结果。

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

How can I mock a base method on an interface that inherits from a generic interface? 如何在继承自通用接口的接口上模拟基本方法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM