查找方法不适用于EF6.1模拟 [英] Find method not working with EF6.1 mock

查看:57
本文介绍了查找方法不适用于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; }
  ...
}

此处中查看其余的设置.

在调试中,我深入到本地" |这个已加载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 ,但这在《模拟》文章中没有提及.

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.

您是否需要像测试双篇文章中那样创建一个Test 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);

在最后一行设置了micContext的地方,还使用了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);

然后,使用您在此处添加的Func作为第二个参数,这可以使Find()操作正确解析.

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模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆