EF4-可以模拟ObjectContext进行单元测试吗? [英] EF4 - possible to mock ObjectContext for unit testing?
问题描述
可以不使用TypeMock Islolator来完成吗?我在网上发现了一些建议,例如传递仅元数据的连接字符串,但是除了TypeMock之外,我所遇到的其他事情似乎都没有真正允许将模拟ObjectContext注入到用于单元测试的服务中.我是否会为TypeMock降低$$费用,还是有其他选择?没有人能创建与开源的TypeMock相当的东西吗?
Can it be done without using TypeMock Islolator? I've found a few suggestions online such as passing in a metadata only connection string, however nothing I've come across besides TypeMock seems to truly allow for a mock ObjectContext that can be injected into services for unit testing. Do I plunk down the $$ for TypeMock, or are there alternatives? Has nobody managed to create anything comparable to TypeMock that is open source?
推荐答案
我正在轻松地对EF4进行单元测试,而无需进行模拟.我所做的就是使用来自
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.
因此,如果需要进行单元测试,请传入InMemoryRepository而不是DataRepository.
Thus if you need to do unit testing, pass in the InMemoryRepository rather than the DataRepository.
这篇关于EF4-可以模拟ObjectContext进行单元测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!