huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
MIT License
132 stars 24 forks source link

mockDbSet.Find not working right #9

Closed jnothstine closed 5 years ago

jnothstine commented 5 years ago

I have set up things pretty much exactly how your example shows, which is pretty slick for mocking a DbContext. Problem is when I call mockDbSet.Object.Find(3) it returns null. When I look at my mockDbSet in the debugger, the Object results shows all of my entities. I have a property named Id in my entity that has the attribute of [Key] but that doesn't see to be doing the trick. If I do this:

mockDbSet.Object.FirstOrDefault(x => x.Id == 3)

I do get my entity. So, it looks to be something related to the [Key] attribute not being observed in the mock. My Id property is a long, don't know if that would make the difference. The Find method works fine when I am running against my regular DbContext with data coming from a database so the problem isn't with the code there.

Any help on this would greatly appreciated.

Thanks,

Jerry

huysentruitw commented 5 years ago

I'll check this out. The reason why it's not yet implemented is because I always advice to use FirstOrDefault instead as Find is not type safe.

huysentruitw commented 5 years ago

Please retest with version 1.0.0.14