Mock实体框架长LINQ查询 [英] Mock Entity Framework long LINQ query

查看:501
本文介绍了Mock实体框架长LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个LINQ查询表达式(下面)想要测试嘲笑实体框架。
要模拟我使用的代码:实体框架测试与模拟框架
这适用于其他查询,但不适用于此

Have a LINQ query expression (below) that want to test mocking the Entity Framework. To mock I am using the code: Entity Framework Testing with a Mocking Framework This works for other queries but doesn't work for this

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中的值

When execute the query the value in '_inner' at this point in the code

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>}
当前:null

结果视图:展开结果视图将枚举IEnumerable

? _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: Expanding the Results View will enumerate the IEnumerable

,当它执行MoveNext()获取异常

and when it executes the MoveNext() get exception


? _inner.MoveNext()
'_inner.MoveNext()'抛出一个异常类型'System.NullReferenceException'
数据:{System.Collections.ListDictionaryInternal}
HResult:-2147467261
HelpLink:null
InnerException:null
消息:对象引用未设置为对象的实例。
资料来源:匿名托管DynamicMethods Assembly
StackTrace:at lambda_method(Closure,<> f__AnonymousType9 2)\r\\\
at
System.Linq .Enumerable。< GroupJoinIterator> d__40
4.MoveNext()\r\\\
at
System.Linq.Enumerable.d__22 3.MoveNext()\\ \\ r \在
System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext()
TargetSite:{Int32 lambda_method(System.Runtime.CompilerServices.Closure,
<> f__AnonymousType9 2 [<> f__AnonymousType7 2 [LL.Core.DataAccess.Models.VwAccountNumberEnt
ity,System.Collections.Generic.IEnumerable `1 [LL.Core.DataAccess.Models.PaymentSummaryEntity]],LL.Core.DataAccess.Models.PaymentSummaryEntity])}

? _inner.MoveNext() '_inner.MoveNext()' threw an exception of type 'System.NullReferenceException' Data: {System.Collections.ListDictionaryInternal} HResult: -2147467261 HelpLink: null InnerException: null Message: "Object reference not set to an instance of an object." Source: "Anonymously Hosted DynamicMethods Assembly" StackTrace: " at lambda_method(Closure , <>f__AnonymousType92 )\r\n at System.Linq.Enumerable.<GroupJoinIterator>d__404.MoveNext()\r\n at System.Linq.Enumerable.d__223.MoveNext()\r\n at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()" TargetSite: {Int32 lambda_method(System.Runtime.CompilerServices.Closure, <>f__AnonymousType92[<>f__AnonymousType72[LL.Core.DataAccess.Models.VwAccountNumberEnt ity,System.Collections.Generic.IEnumerable`1[LL.Core.DataAccess.Models.PaymentSummaryEntity]],LL.Core.DataAccess.Models.PaymentSummaryEntity])}

尝试了几个更改,包括在这里建议的
DbS et mock,在第二次调用ToList时没有结果

Tried few changes including the one suggested in here DbSet mock, no results while calling ToList secondly

但仍然会遇到同样的问题。请问,有人可以点亮吗?

but still get the same problem. Please, can anybody give some light?

验证查询实现与集成测试不是一个选择,因为老板希望这在单元测试级别进行测试,因为所有其他查询

Verify query implementation with integration tests is not an option, as boss wants this to be tested at unit test level as all the other queries.

更新:
感谢@Eduard Malakhov这是尝试的东西。如果删除'DefaultIfEmpty'并运行查询,它不会抛出异常。但是如何避免使用DefaultIfEmpty的异常?如何修改嘲笑代码来检查这种情况,因为不是没有发生嘲笑的电话的例外?

UPDATE: Thanks @Eduard Malakhov this is something tried. If remove the both 'DefaultIfEmpty' and run the query it doesn't throw the exception. But how can avoid the exception with the DefaultIfEmpty? How can I modify the mocking code to check this case, as the exception is not happening with a not mocked call?

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,
};


推荐答案

我的黑客解决方案(是星期五,回家 :)。删除异常,但遗憾的是没有给出正确的结果。

My hacky 'solution' (is Friday and want to go home :). Removes the exception but unfortunately, doesn't give right results.

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

这篇关于Mock实体框架长LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆