Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core 如何模拟DbUpdateConcurrencyException_Entity Framework Core_Moq - Fatal编程技术网

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()}
工作正常!如果你把它作为一个答案,我会接受。谢谢你的帮助!