模拟Entity Framework长LINQ查询

3

我有一个LINQ查询表达式(如下),想要测试模拟Entity Framework。为了模拟,我使用以下代码:使用模拟框架进行Entity Framework测试。这适用于其他查询,但对于此查询不起作用。

var query = from vwPs in _reportingDbContext.VwAccountNumber
join ps in _reportingDbContext.PaymentSummary
  on new
  {
      Key1 = vwPs.Id,
      Key2 = vwPs.AccountNumber,
      Key3 = true,
      Key4 = true
  }
     equals
     new
     {
         Key1 = ps.Id,
         Key2 = ps.AccountNumber,
         Key3 = ps.PaymentDate >= fromDateTime,
         Key4 = ps.PaymentDate < toDateTime
     }
     into viewJoinPayment
from ps in viewJoinPayment.DefaultIfEmpty()
join prg in generatedReportIds
     on ps.PaymentRef equals prg
     into paymentJoinGenerated
from prgRow in paymentJoinGenerated.DefaultIfEmpty()
where vwPs.Id == Id
select new
{
    vwPs.AccountNumber,
    Payments = ps,
    ReportGenerated = prgRow
};

执行查询时,代码中此时“_inner”变量的值。
internal class TestDbAsyncEnumerator<T> : IDbAsyncEnumerator<T> {
    //...
    public Task<bool> MoveNextAsync(CancellationToken cancellationToken) 
    { 
        return Task.FromResult(_inner.MoveNext()); 
    }
    //...

}

? _inner {System.Linq.Enumerable.WhereSelectEnumerableIterator<<>f__AnonymousType11<<>f__AnonymousType10<<>f__AnonymousType9<<>f__AnonymousType7>, LL.Core.DataAccess.Models.PaymentSummaryEntity>, System.Collections.Generic.IEnumerable>, int>, <>f__AnonymousType12>} Current: null
Results View: 展开“结果视图”将枚举IEnumerable

并且在执行MoveNext()时出现异常

? _inner.MoveNext() '_inner.MoveNext()'引发了类型为'System.NullReferenceException'的异常 Data: {System.Collections.ListDictionaryInternal} HResult: -2147467261 HelpLink: null InnerException: null Message: "对象引用未设置为对象的实例。" Source: "Anonymously Hosted DynamicMethods Assembly" StackTrace: " 在lambda_method(Closure ,<>f__AnonymousType92)\r\n 在System.Linq.Enumerable.<GroupJoinIterator>d__404.MoveNext()\r\n 在System.Linq.Enumerable.d__223.MoveNext()\r\n 在System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()" TargetSite: {Int32 lambda_method(System.Runtime.CompilerServices.Closure, <>f__AnonymousType92[<>f__AnonymousType72[LL.Core.DataAccess.Models.VwAccountNumberEntity,System.Collections.Generic.IEnumerable`1[LL.Core.DataAccess.Models.PaymentSummaryEntity]],LL.Core.DataAccess.Models.PaymentSummaryEntity])}

试过一些更改,包括这里建议的一个 DbSet mock, no results while calling ToList secondly

但仍然存在相同的问题。请问有人可以给出一些提示吗?

更新: 谢谢@Eduard Malakhov,这是尝试过的内容。如果删除“DefaultIfEmpty”并运行查询,则不会引发异常。但是如何避免使用DefaultIfEmpty引发异常?我该如何修改模拟代码以检查此情况,因为未模拟的调用不会发生异常?

var query = from vwPs in _charityReportingDbContext.VwCharityAccountNumber
join ps in _charityReportingDbContext.PaymentSummary
  on new
  {
      Key1 = vwPs.CharityId,
      Key2 = vwPs.AccountNumber,
      Key3 = true,
      Key4 = true
  }
     equals
     new
     {
         Key1 = ps.CharityId,
         Key2 = ps.AccountNumber,
         Key3 = ps.PaymentDate >= fromDateTime,
         Key4 = ps.PaymentDate < toDateTime
     }
     into viewJoinPayment
select new
{
    vwPs.AccountNumber,
};

1
只是猜测:在该语句中,您两次调用了 **DefaultIfEmpty()**,如果相应的可枚举对象为空,则会导致空引用,并可能随后导致 NullReferenceException。如果您删除这些调用并改为使用空的可枚举对象,会怎样呢? - Eduard Malakhov
@EduardMalakhov 谢谢,你的解释帮助我决定实现下面的“解决方案”。也许模拟EF不是正确的方法,应该像其他帖子中建议的那样使用内存数据库。 - Ricardo stands with Ukraine
1
我希望你的老板对此有一个受过教育的看法。我的看法是嘲笑EF是没有希望的。测试构建了一个与真实数据访问世界根本不同的梦幻世界。在数据库中执行的LINQ查询作为SQL执行,而LINQ-to-object查询会抛出NRE,这只是其中一个重要的区别。当涉及到测试EF函数时,请使用集成测试。其余部分使用单元测试。 - Gert Arnold
@Riga,你有没有找到其他方法来模拟包含DefaultIfEmpty()的linq查询。我也遇到了同样的问题,无法模拟这些情况下的查询。 - Dinesh M
1
最终决定使用存储过程。我们没有找到其他解决方案。 - Ricardo stands with Ukraine
1个回答

0

我的“hacky”解决方案(今天是星期五,想回家:)。虽然消除了异常,但不幸的是,没有给出正确的结果。

public Task<bool> MoveNextAsync(CancellationToken cancellationToken){ 
try
{
    return Task.FromResult(_inner.MoveNext());
}
catch (Exception ex)
{
    return Task.FromResult(false);
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接