Question

I'm trying to unit test an async method in EF 6.0.2 with Moq 4.0.10827 and I'm getting blocked in what appears to be a casting issue.

The following works fine:

public async Task<List<Testimonial>> GetByEventIdAsync(int eventId)
{
    var query = from t in _context.Testimonials
                orderby t.Ordinal
                select t;
    var result = query
        .ToListAsync()
        .ConfigureAwait(false);
    return await result;
}

but the following breaks (added a where clause)

public async Task<List<Testimonial>> GetByEventIdAsync(int eventId)
{
    var query = from t in _context.Testimonials
                where t.EventId == eventId
                orderby t.Ordinal
                select t;
    var result = query
        .ToListAsync()
        .ConfigureAwait(false);
    return await result;
}

I did some debugging and found the following

var q1 = _context.Testimonials; // Type = Castle.Proxies.IDbSet`1Proxy_1
var q2 = q1.Where(t => t.EventId == eventId); // Type = TestDbAsyncEnumerable`1 (from linked code)
var q3 = q2.OrderBy(o => o.Ordinal); // Type = System.Linq.EnumerableQuery`1

Why is the class double disappearing? And how do I fix it?

UPDATE: I also found that if I add the where back and remove the orderby, that also works, so the issue seems to be having multiple clauses.

Was it helpful?

Solution

Finally found the answer here.

IQueryable needed to be added like so:

public class TestDbAsyncEnumerable<T> 
    : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top