Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The source IQueryable doesn't implement IDbAsyncEnumerable when trying to mock

I am trying to test a bit of code I have:

public async Task<Sortation> SaveAsync(Sortation sortation)
{
    if (sortation.Id == 0)
    {
        var sortations = await ListAsync(sortation.CategoryId);
        sortation.Order = sortations.Count;
        _sortationService.Create(sortation);
    }
    else
    {
        _sortationService.Update(sortation);
    }

    await _sortationService.SaveChangesAsync();

    return sortation;
}

The ListAsync method is causing me an issue. I set up my test like this:

[Test]
public async Task ShouldHaveOrderOfZero()
{
    // Assemble
    const string categoryId = "cameras";
    var services = SortationContext.GivenServices();
    var sortationProvider = services.WhenGetSortationProvider();
    var sortations = new List<Sortation>();
    var sortation = new Sortation { CategoryId = categoryId };

    services.MockSortationService.Setup(x => x.List()).Returns(sortations.AsQueryable);

    // Act
    await sortationProvider.SaveAsync(sortation);

    // Assert
    sortation.Order.Should().Be(0);
}

And when I run this, I get this error:

Message: System.InvalidOperationException : The source IQueryable doesn't implement IDbAsyncEnumerable. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations.

According to this: Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations I need to add EF to my UnitTest project, which I did. But the error still persists.

The ListAsync method looks like this:

public async Task<List<Sortation>> ListAsync(string categoryId, params string[] includes) => 
    await _sortationService.List(includes).Where(m => m.CategoryId.Equals(categoryId)).ToListAsync();

Does anyone know how I can stop this error from happening?

like image 933
r3plica Avatar asked Oct 17 '25 17:10

r3plica


1 Answers

In my case the exception was caused by using the wrong ToListAsync extension.

It came from:

using System.Data.Entity; instead of

using Microsoft.EntityFrameworkCore; Changing the namespace fixed the error.

like image 143
Navid Developer Avatar answered Oct 20 '25 08:10

Navid Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!