有一个LINQ查询表达式(如下),该表达式要测试模拟实体框架。为了模拟,我使用代码:Entity Framework Testing with a Mocking Framework这适用于其他查询,但不适用于此var query = from vwPs in _reportingDbContext.VwAccountNumberjoin 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 viewJoinPaymentfrom ps in viewJoinPayment.DefaultIfEmpty()join prg in generatedReportIds on ps.PaymentRef equals prg into paymentJoinGeneratedfrom prgRow in paymentJoinGenerated.DefaultIfEmpty()where vwPs.Id == Idselect 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()); } //...} ? _内 {System.Linq.Enumerable.WhereSelectEnumerableIterator f__AnonymousType11 f__AnonymousType10 f__AnonymousType9 f__AnonymousType7>,LL.Core.DataAccess.Models.PaymentSummaryEntity>,System.Collections.Generic.IEnumerable>, > f__AnonymousType12>} 当前:null 结果视图:展开结果视图将枚举IEnumerable并且在执行MoveNext()时获取异常 ? _inner.MoveNext() '_inner.MoveNext()'引发了类型'System.NullReferenceException'的异常 数据:{System.Collections.ListDictionaryInternal} 结果:-2147467261 帮助链接:null InnerException:null 消息:“对象引用未设置为对象的实例。” 来源:“匿名托管的DynamicMethods程序集” StackTrace:“位于lambda_method(Closure, f__AnonymousType9 2 )\r\n at System.Linq.Enumerable.<GroupJoinIterator>d__40 4.MoveNext()\ r \ n位于 System.Linq.Enumerable.d__22 3.MoveNext()\r\n at 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])}尝试了一些更改,包括此处建议的更改DbSet mock, no results while calling ToList secondly但仍然遇到相同的问题。拜托,有人可以给点儿光吗?不能选择使用集成测试来验证查询的实现,因为老板希望像其他所有查询一样在单元测试级别对此进行测试。更新:感谢@Eduard Malakhov,这是尝试过的方法。如果同时删除两个“ DefaultIfEmpty”并运行查询,则不会引发异常。但是如何避免DefaultIfEmpty的异常?我如何修改模拟代码以检查这种情况,因为未模拟调用不会发生异常?var query = from vwPs in _charityReportingDbContext.VwCharityAccountNumberjoin 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 viewJoinPaymentselect new{ vwPs.AccountNumber,}; (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我骇人听闻的“解决方案”(星期五,想回家:)。删除异常,但不幸的是,没有给出正确的结果。public Task<bool> MoveNextAsync(CancellationToken cancellationToken){try{ return Task.FromResult(_inner.MoveNext());}catch (Exception ex){ return Task.FromResult(false);}} (adsbygoogle = window.adsbygoogle || []).push({}); 09-28 14:03