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