EF4.1 DbSet与EF4 ObjectContext和单元测试 [英] EF4.1 DbSet vs. EF4 ObjectContext and Unit Testing

查看:172
本文介绍了EF4.1 DbSet与EF4 ObjectContext和单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个我已经开始使用EF4的项目,并且在事实之后我将继续添加单元测试。我使用我的模型使用 EF4 POCO T4模板(数据库)第一上下文。我正在为我的DAC逻辑使用通用存储库,并为持久性使用工作模式单位。



但是,我遇到一些问题,了解如何模拟ObjectContext /对象集。我使用 FakeObjectSet< T> 样本/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspxrel =nofollow>这篇文章,但它仍然留下了一些事情,如自动递增身份和交易回滚。



所以,我试图找到一个很好的EF设计,仍然可以完全单元测试。



我的问题是,EF4.1 DbSet是否解决了很多单元测试EF4的问题?有没有什么好的综合文章设计EF4.1解决方案是完全可测试的?



另外,请记住,我需要一个模型的第一个解决方案。



提前感谢

解决方案

完全模拟嘲笑层的行为不是单元测试。单元测试的观点认为,嘲笑层只是起作用。单元测试验证测试方法不是模拟。您将仅验证模拟器上正确的方法是否被调用,如果您的业务逻辑需要某些值,那么您可能会设置一些回调来修改传递的数据。示例:



您有一种业务方法将记录插入数据库,并在插入后使用实体及其ID。 IObjectSet mock将被配置为:




  • 设置 AddObject 仅被调用一次 - 您可以在验证中设置预期的实例

  • 您可以定义 AddObject 将Id设置为某个值,然后在测试中使用它。



DbSet 不会有任何区别 - 它只是围绕 ObjectSet 包装,具有相似的行为。在我看来,没有任何有效的方式让嘲笑行为真正的EF。用模拟EF +数据库的行为创建模拟所需的努力将会比您的整个应用程序的努力大得多!那将不会再嘲笑它将是假的EF提供商。



如果要测试您的EF代码(映射,查询,持久化)和数据库行为(如自动递增,事务等),则必须编写集成测试。在这里,您有一些相关问题,讨论存储库,工作单元和测试中的挑战:




I currently have a project I've started with EF4, and am going back and adding in unit testing after the fact. I am using the EF4 POCO T4 templates with my model (database) first context. I am using generic repositories for my DAC logic, and unit of work pattern for persistence.

However, I'm running into some issues understanding how to mock up the ObjectContext/ObjectSet. I looked at using the FakeObjectSet<T> sample from this article, but it still leaves a few things out, such as auto-incrementing identities and transaction rollbacks.

So, I'm trying to find a good EF design that will still be fully unit testable.

My question is, does EF4.1 DbSet solve a lot of the issues w/ unit testing EF4? Are there any good comprehensive articles for designing EF4.1 solutions that are fully testable?

Also, keep in mind that I need a model-first solution.

Thanks in advance.

解决方案

Fully simulating behavior of mocked layer is not point of unit testing. The point of unit testing is believing that mocked layer simply works. Unit test verifies the tested method not the mock. You will just verify that correct method on the mock were called and perhaps you will set up some callbacks for modifying passed data if your business logic expects some values. Example:

You have a business method inserting the record to the database and using the entity and its Id after insertion. The IObjectSet mock will be configured to:

  • Set expectation that AddObject was called only once - you can set the expected instance in the verification
  • You can define callback for AddObject to set Id to some value and use it later in the test

DbSet will not make any difference - it is just wrapper around ObjectSet with similar behavior. In my opinion there is no efficient way to make mocks behave as real EF. The effort needed for creating mock with behavior simulating EF + database will be much bigger then effort for your whole application! That will not be mock anymore it will be fake EF provider.

If you want to test your EF code (mapping, querying, persisting) and database behavior (like auto-increment, transactions, etc) you have to write integration tests. Here you have some related questions discussing repositories, unit of work and challenges with testing:

这篇关于EF4.1 DbSet与EF4 ObjectContext和单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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