Entity framework core 如何模拟DbUpdateConcurrencyException
我正在尝试模拟服务中的函数,以抛出一个Entity framework core 如何模拟DbUpdateConcurrencyException,entity-framework-core,moq,Entity Framework Core,Moq,我正在尝试模拟服务中的函数,以抛出一个DbUpdateConcurrencyException。我的代码只需要检查类型为DbUpdateConcurrencyException的异常,而不需要读取构造函数要求的异常消息或条目列表 我想通过调用DbUpdateConcurrencyException的无参数构造函数来设置Mock,但这在EFCore中不存在 var mockService = new Mock<IMyService>(); mockService.Setup(servi
DbUpdateConcurrencyException
。我的代码只需要检查类型为DbUpdateConcurrencyException
的异常,而不需要读取构造函数要求的异常消息或条目列表
我想通过调用DbUpdateConcurrencyException
的无参数构造函数来设置Mock
,但这在EFCore中不存在
var mockService = new Mock<IMyService>();
mockService.Setup(service => service.UpdateFooAsync(It.IsNotNull<Data.Foo>())).Throws(new DbUpdateConcurrencyException());
new DbUpdateConcurrencyException(“,new List())
给出:
Message: System.ArgumentNullException : Value cannot be null.
Parameter name: entries
Message: System.ArgumentException : The collection argument 'entries' must contain at least one element.
在Moq中有没有一种方法可以让我模拟
DbUpdateConcurrencyException
,而不必经过构造函数的检查?根据您在注释中共享的文档,您应该使用带有两个参数的ctor。诀窍是提供非空的字符串
和非空的列表
,moq
可以帮助您,例如
new DbUpdateConcurrencyException(string.Empty, new List<IUpdateEntry>{Mock.Of<IUpdateEntry>()});
newdbupdateconcurrencyException(string.Empty,新列表{Mock.Of()});
为什么你认为无参数ctor不存在https://github.com/aspnet/EntityFrameworkCore/blob/master/src/EFCore/DbUpdateConcurrencyException.cs
?仅显示双参数构造函数。当我试图调用new DbUpdateConcurrencyException()
时,Visual Studio也会抱怨说没有给出与“DbUpdateConcurrencyException.DbUpdateConcurrencyException(string,IReadOnlyList)”的必需形式参数“message”对应的参数。
Hm.:/。你能试着传递新列表{Mock.Of()}的字符串吗?它们不应为null…String.Empty,新列表{Mock.Of()}
工作正常!如果你把它作为一个答案,我会接受。谢谢你的帮助!