What do I replace IQueryable with for Async operations?

c# entity-framework-core linq

Expert Answer

Due to your mocking strategy, your fake provider has only returned.panels for Query , andpanels is a straightforward object that LINQ-to-Objects exposes as being queryable:

private readonly IQueryable<Panel> panels = new List<Panel>() { panel }.AsQueryable();

This does not, in fact, implementIAsyncQueryProvider . If you can get the regular query provider, you should be able to spoof it by wrapping it in a false always-synchronous version.return Task.FromResult(Execute(expression)) ), but I honestly don't know whether this would be a meaningful test because you're omitting so many crucial elements ofasync that it most likely isn't worthwhile.

4
6/25/2018 12:34:27 PM

Popular Answer

I ran across this problem today and this library entirely solved it for me, please see:

Making Entity Framework Core operations like FirstOrDefaultAsync and ToListAsync

//1 - create a List<T> with test items
var users = new List<UserEntity>()
{
  new UserEntity{LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012")},
  ...
};

//2 - build mock by extension
var mock = users.AsQueryable().BuildMock();

//3 - setup the mock as Queryable for Moq
_userRepository.Setup(x => x.GetQueryable()).Returns(mock.Object);

//3 - setup the mock as Queryable for NSubstitute
_userRepository.GetQueryable().Returns(mock);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow