13

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();
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
janhartmann
  • 14,713
  • 15
  • 82
  • 138
  • 2
    Have a look here http://stackoverflow.com/a/40500030/5233410 solution can be modified and applied to your use case – Nkosi Nov 23 '16 at 11:36
  • Seems like that code is working on the `DbSet`, but I'll have a look. – janhartmann Nov 23 '16 at 11:43
  • Yes it does, which is also of type IQueriable. the same strategy can be applied. Or you could look into the AsAsyncEnumerable extension method used internally in the ef source code. – Nkosi Nov 23 '16 at 11:45
  • The difference is that they are testing directly on the `DbContext/DbSet`, where I am mocking an interface which has a method returning `IQuerable`. – janhartmann Nov 23 '16 at 11:47
  • @Nkosi you were right, I came up with a fix. :-) Hold on, I'll post the answer. – janhartmann Nov 23 '16 at 12:40

1 Answers1

16

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();
Community
  • 1
  • 1
janhartmann
  • 14,713
  • 15
  • 82
  • 138