我有一个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,
};