EF6 DbSet<T> returns null in Moq

最后都变了- 提交于 2019-12-05 04:09:17
Patrick Quirk

Add a setup for the Set<T>() method:

mockContext.Setup(c => c.Set<CartItem>()).Returns(mockSet.Object);

Even though on the real EFContext the property Cart and Set<CartItem>() refer to the same object, the mock of the context doesn't know that, so you need to tell it explicitly what to return.

Since it was a loose mock, the call to a method that hasn't been setup returns the default value, which in this case is null. Strict mocks are nice in helping find this error, but also have maintenance costs that other folks don't want to deal with.

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