EF4 - possible to mock ObjectContext for unit testing?

怎甘沉沦 提交于 2019-11-30 13:49:34

I'm unit testing EF4 easily without mocking. What I did was create a repository interface using the code from http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/ as a basis I then created an InMemoryRepository<T> class that used the IRepository interface. I then replaced the IObjectSet<T> with a List<T> inside of the class and changed the retrieval methods accordingly.

Thus if you need to do unit testing, pass in the InMemoryRepository rather than the DataRepository.

Put your Linq2Entity query behind an interface, unit test it in isolation against a real database.

Write tests for your business logic with mocks for your query interfaces. Don't let Linq bleed into your business logic!

Don't use the RepositoryPattern!

Wrap the ObjectContext in a proxy class. Then inject that into your classes.

kenno

I don't think the repository pattern is the only answer to the question (it avoids the problem, sure)

I liked this answer - I think more appropriate for introducing tests to an existing codebase Creating Interface for ObjectContext

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