Find method not working with EF6.1 mock

半城伤御伤魂 提交于 2021-02-17 03:59:07

问题


I've setup mocking using these msdn guidlelines:

Testing with a mocking framework (EF6 onwards)

var bsAc = _db.BusAcnts.FirstOrDefault(i => i.Id == 1);

returns an account but

var bsAc = _db.BusAcnts.Find(1);  

returns null when mocked. Find only fails when testing with a mock, it works fine in production.

BusAcnt: (Id is the Primary Key)

public class BusAcnt
{
  public int Id { get; set; }
  ...
}

See the rest of my setup here.

In debug I drilled down into Locals | this | MyDbContext and all the mocked accounts are loaded and FirstOrDefault returns the expected account.

In a companion article on doubles:

Testing with your own test doubles (EF6 onwards)

They talk about implementing Find but this is not mentioned in the Mocking article.

Has anyone else managed to get the Find method working with mocking?

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.

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?


回答1:


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.

Here is the breakdown:

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);

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);

This then allows for the Find() operation to resolve correctly, using the Func that you have added as the second parameter here.

This StackOverflow post helped me reach my intended goal: https://stackoverflow.com/a/32443711




回答2:


just yesterday i encountered the same difficulties.

I found no solution for the problem, that the find method on the mocked dbset doesn't work, it always claims the dbset shall be null, but which in fact can't be true, because the same mocking code will work with a Where or a Single Expression instead of Find.

That also was my workaround. I changed Find to Single.




回答3:


Use EntityFrameworkMock.Moq, this will abstract all the mocking problems.



来源:https://stackoverflow.com/questions/23554902/find-method-not-working-with-ef6-1-mock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!