简体   繁体   中英

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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