Moq异步DbSet设置IDbAsyncQueryProvider

问题描述 投票:0回答:1

我有这个最小起订量设置:

_mockContext.Setup(x => x.CarSections).Returns(new List<CarSection> { _carSection }.ToDbSet());

基本上将List分配给DbContext.CarSections的实体。

其中一种方法是异步方法。类似于:

public async Task<CarSection> GetSectionAsync(int sectionId)
    {
        return await _context
            .CarSections
            .FirstOrDefaultAsync(s => s.CarSectionId == sectionId && s.StatusCode == 4);
    }

通过该方法时,我得到了错误:

*The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations.*

我相信这是因为.ToDbSet()。已经使用Task.FromResult(new List<CarSection>{ _carSection }尝试过,但是.Returns期望使用“ DbSet”而不是“ Task”。

有关如何解决此问题的任何想法?

c# unit-testing nunit moq
1个回答
0
投票

GetSectionAsync应该返回单个车厢对象

var carSection = new CarSection();
_mockContext
   .Setup(x => x.GetSectionAsync(It.IsAny<int>())) // or provide expected id value
   .Returns(Task.FromResult(carSection)); // return task from single car section
© www.soinside.com 2019 - 2024. All rights reserved.