Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting IQueryable to implement IAsyncEnumerable [duplicate]

I have a query in a method:

private readonly IEntityReader<Customer> _reader;

public async Task<IEnumerable<Customer>> HandleAsync(GetCustomer query)
{
    var result = _reader.Query()
        .Include(customer => customer.Organization)
        .Where(customer => customer.Name == query.Name);

    return await result.ToListAsync();
}

Which has this unit-test:

[Fact]
public async Task HandleGetCustomer_ReturnsCustomer_WhenNameMatches()
{
    // Arrange
    var customers = new List<Customer>()
    {
        new Customer
        {
            Id = new Guid("d4e749ba-6874-40f4-9134-6c9cc1bc95fe"),
            Name = "John Doe",
            Age = 18,
            Organization = new Organization
            {
                Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
                Name = "TheCompany",
                Number = 42
            }
        },
        new Customer
        {
            Id = new Guid("0679ceb5-3d4f-41f3-a1b0-b167e1ac6d7e"),
            Name = "Another Guy",
            Age = 39,
            Organization = new Organization
            {
                Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
                Name = "TheCompany",
                Number = 42
            }
        }
    }.AsQueryable();

    var entityReader = new Mock<IEntityReader<Customer>>(MockBehavior.Strict);
    entityReader.Setup(reader => reader.Query()).Returns(customers);

    var query = new GetCustomer
    {
        Name = "John Doe"
    };

    var handler = new HandleGetCustomer(entityReader.Object);

    // Act
    var result = await handler.HandleAsync(query);

    // Assert
    Assert.NotNull(result);
    Assert.True(result.Count() == 1);
    Assert.True(result.FirstOrDefault().Id == new Guid("d4e749ba-6874-40f4-9134-6c9cc1bc95fe"));
}

By apperently the call to .ToListAsync(); fails because of:

The source IQueryable doesn't implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.

The extension method is a method from Entity Framework Core.

How can I convert my list of customers to the IAsyncEnumerable?

For reference, the IEntityReader interface is defined such as:

public interface IEntityReader<out TEntity> where TEntity : Entity
{
    IQueryable<TEntity> Query();
}
like image 317
janhartmann Avatar asked Nov 23 '16 11:11

janhartmann


1 Answers

Thanks to @Nkosi, this is the solution:

The code snippet of How to mock an async repository with Entity Framework Core with the change of the customer list:

var customers = new TestAsyncEnumerable<Customer>(new List<Customer>
{
    new Customer
    {
        Id = new Guid("d4e749ba-6874-40f4-9134-6c9cc1bc95fe"),
        Name = "John Doe",
        Age = 18,
        Organization = new Organization
        {
            Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
            Name = "TheCompany",
            Number = 42
        }
    },
    new Customer
    {
        Id = new Guid("0679ceb5-3d4f-41f3-a1b0-b167e1ac6d7e"),
        Name = "Another Guy",
        Age = 39,
        Organization = new Organization
        {
            Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
            Name = "TheCompany",
            Number = 42
        }
    }
}.AsQueryable();
like image 181
janhartmann Avatar answered Oct 05 '22 02:10

janhartmann