如何使用已被查询为 IQurable 的 ToListAsync 变量设置等待最小起订量?

How to Moq setup for await with ToListAsync variable which is already queried as IQurable?

IQueryable 类型列表的变量,

var requests = _genericRepository.GetIQueryable<RequestMaster>();

所以对于上面的代码行,我下面的模拟设置工作正常

private IEnumerable<RequestMaster> FakeRequestMaster()
{
  string fakeData = @"[{
  'RequestId': '118',
  'RequestUuid':'901358ab-32e3-4a52-9182-ba68a0a03b50',
  'UserId':'klj123kj-0120-4f3c-8d29-asd213klj213lkl',
  'StatusId':'7',
  'CreatedOn':'11/28/2021 10:01:42 AM',
  'CreatedBy':'000000-0000-0000-0000-000000000000',
  'ModifiedOn':'3/23/2022 6:30:31 PM',
  'ModifiedBy':'000000-0000-0000-0000-000000000000'
  'IsActive':'true',
 }]";
 return JsonConvert.DeserializeObject<IEnumerable<RequestMaster>>(fakeData);
}

[Fact]
public async Task GetRequests_SendingValidId_ExpectedNotNull()
{
    var fakeRequestMaster = FakeRequestMaster();
    _mockGenericRepository.Setup(s => s.GetIQueryable<RequestMaster>()).Returns(fakeRequestMaster.AsQueryable);
    var result = await service.GetRequests(typeId);
    
    // Assert
    Assert.NotNull(result);
}

但是在通过“await”和“ToListAsync”获取一些数据之后,然后在这一步测试编译器跳过执行并抛出错误:

System.InvalidCastException : Unable to cast object of type 'System.Linq.EnumerableQuery 1[TMo.MWav.Data.MicrowaveModels.RequestMaster]' to type 'System.Collections.Generic.IAsyncEnumerable 1[TMo.MWav.Data.MicrowaveModels.RequestMaster]'.

所以,我尝试了link中提到的:

var mockSetDb = new Mock<DbSet<RequestMaster>>();
mockSetDb.As<IAsyncEnumerable<RequestMaster>>().Setup(m => m.GetEnumerator()).Returns(new TestAsyncEnumerator<RequestMaster>(fakeRequestMaster.GetEnumerator()));
mockSetDb.As<IQueryable<RequestMaster>>().Setup(m => m.Provider).Returns(new TestAsyncQueryProvider<RequestMaster>(fakeRequestMaster.Provider));
mockSetDb.As<IQueryable<RequestMaster>>().Setup(m => m.Expression).Returns(fakeRequestMaster.Expression);
mockSetDb.As<IQueryable<RequestMaster>>().Setup(m => m.ElementType).Returns(fakeRequestMaster.ElementType);
mockSetDb.As<IQueryable<RequestMaster>>().Setup(m => m.GetEnumerator()).Returns(() => fakeRequestMaster.GetEnumerator());

所以这也不起作用,出现问题:

'IAsyncEnumerable' does not contain a definition for 'GetEnumerator' and no accessible extension method 'GetEnumerator' accepting a first argument of type 'IAsyncEnumerable' could be found (are you missing a using directive or an assembly reference?)

'IEnumerable' does not contain a definition for 'Provider' and no accessible extension method 'Provider' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

'IEnumerable' does not contain a definition for 'Expression' and no accessible extension method 'Expression' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

'IEnumerable' does not contain a definition for 'ElementType' and no accessible extension method 'ElementType' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

所以,谁能帮我 Moq 设置等待 ToListAsync 变量。

通过安装MockQueryable.Moq 5.0 包

使用 MockQueryable.Moq;

var requestMaster = RequestMaster();
var mock = requestMaster.BuildMock();
_mockGenericRepository.Setup(s => s.GetIQueryable<RequestMaster>()).Returns(mock);

工作正常。