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.
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