EF4 - 可以模拟 ObjectContext 进行单元测试吗? [英] EF4 - possible to mock ObjectContext for unit testing?

查看:20
本文介绍了EF4 - 可以模拟 ObjectContext 进行单元测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用 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 进行单元测试.我所做的是使用 http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/ 作为基础然后我创建了一个使用 IRepository 接口的 InMemoryRepository<T> 类.然后,我将 IObjectSet<T> 替换为类中的 List<T> 并相应地更改了检索方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆