gpt4 book ai didi

c# - 模拟 Entity Framework 长 LINQ 查询

转载 作者:太空宇宙 更新时间:2023-11-03 15:09:18 25 4
gpt4 key购买 nike

有一个 LINQ 查询表达式(如下)想要测试模拟 Entity Framework 。为了模拟我正在使用代码:Entity Framework Testing with a Mocking 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: Expanding the Results View will enumerate the IEnumerable

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

? _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__40
4.MoveNext()\r\n at System.Linq.Enumerable.d__223.MoveNext()\r\n at
System.Linq.Enumerable.WhereSelectEnumerableIterator
2.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])}

尝试了一些更改,包括此处建议的更改 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,
};

最佳答案

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

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

关于c# - 模拟 Entity Framework 长 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41899177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com