Find method not working with EF6.1 mock(查找不适用于 EF6.1 模拟的方法)
问题描述
我已经使用这些 msdn 指导线设置了模拟:
I've setup mocking using these msdn guidlelines:
使用模拟框架进行测试(从 EF6 开始)
var bsAc = _db.BusAcnts.FirstOrDefault(i => i.Id == 1);
返回一个帐户,但
var bsAc = _db.BusAcnts.Find(1);
模拟时返回 null.Find
仅在使用模拟测试时失败,它在生产中运行良好.
returns null when mocked. Find
only fails when testing with a mock, it works fine in production.
BusAcnt:(Id 是主键)
BusAcnt: (Id is the Primary Key)
public class BusAcnt
{
public int Id { get; set; }
...
}
查看我的其余设置这里.
在调试中,我深入了解了 Locals |这个 |MyDbContext 和所有模拟的帐户都已加载,FirstOrDefault
返回预期的帐户.
In debug I drilled down into Locals | this | MyDbContext and all the mocked accounts are loaded and FirstOrDefault
returns the expected account.
在双打的配套文章中:
使用您自己的测试替身进行测试(从 EF6 开始)
他们谈论实现 Find
但这在 Mocking 文章中没有提到.
They talk about implementing Find
but this is not mentioned in the Mocking article.
有没有其他人设法让 Find
方法与模拟一起工作?
Has anyone else managed to get the Find
method working with mocking?
有没有其他人遇到过同样的问题,这是 EF6.1 模拟的问题还是我的代码错误?我很想听听其他人关于他们使用 Find
方法进行模拟的经验.
Has anyone else ran into this same problem, is it an issue with EF6.1 mocking or a code error on my part? Please I'm interested in hearing from others on what their experience with mocking with the Find
method has been.
您是否需要像测试双重文章中那样创建测试 DbSet?模拟文章中设置的语法是什么?
Do you need to create a Test DbSet as in the test double article? What would the syntax be for the setup in the mocking article?
推荐答案
对于任何通过搜索偶然发现此页面并使用 Moq 框架的人,我有一个关于如何让 Find 方法按预期工作的建议.
For anyone stumbling upon this page from searching, and is using the Moq framework, I have a suggestion for how to get the Find method working as expected.
这是细分:
首先,您必须使用 Moq 并在您的测试项目中包含EntityFrameworkTesting.Moq"包.
First, you must be using Moq and have the 'EntityFrameworkTesting.Moq' package in your test project.
在代码中,当设置你的模拟上下文和数据集时,你可能会有类似这样的东西:
In the code, when setting up your mock Context and data sets, you will likely have something similar to this:
var users = new List<User>
{
new User
{
UserId=1,
UserName="testUser@example.com"
},
new User
{
UserId=5,
UserName="otherUser@example.com"
}
};
var mockContext = new Mock<MyContext>();
mockContext.Setup(x => x.Users)
.Returns(new Mock<DbSet<User>>().SetupData(users).Object);
最后一行,mockContext 设置的地方,也接受了第二个 Func 类型的参数,用于解析 Find 方法.这条线的变化如下:
That last line, where the mockContext is set up, also takes a second parameter of type Func that is used to resolve the Find method. Here's how this line changes:
mockContext.Setup(x => x.Users)
.Returns(new Mock<DbSet<User>>().SetupData(users, o => {
return users.Single(x => x.UserId == (int)o.First());
}).Object);
这允许 Find() 操作正确解析,使用您在此处添加为第二个参数的 Func.
This then allows for the Find() operation to resolve correctly, using the Func that you have added as the second parameter here.
这篇 StackOverflow 帖子帮助我实现了预期目标:https://stackoverflow.com/a/32443711
This StackOverflow post helped me reach my intended goal: https://stackoverflow.com/a/32443711
这篇关于查找不适用于 EF6.1 模拟的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:查找不适用于 EF6.1 模拟的方法
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01