简体   繁体   English

如何使用多个子句对Entity Framework的异步方法进行单元测试

[英]How to unit test async methods of Entity Framework with a multiple clauses

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. 我正在尝试使用Moq 4.0.10827对EF 6.0.2中的异步方法进行单元测试,并且我看起来像是一个转换问题。

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) 但以下休息时间(添加了where子句)

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. 更新:我还发现如果我添加where返回并删除orderby ,这也有效,所以问题似乎有多个子句。

Finally found the answer here. 终于在这里找到了答案

IQueryable needed to be added like so: 需要像这样添加IQueryable:

public class TestDbAsyncEnumerable<T> 
    : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM