简体   繁体   English

单元测试业务逻辑层

[英]Unit test business logic layer

I'm starting to introduce Formal Unit Testing in our company as we are having a project that's becoming bigger and bigger and on this project another guy is going to help me. 我正在开始在我们公司引入正式单元测试,因为我们正在建立一个越来越大的项目,在这个项目中,另一个人会帮助我。 So I need to be sure that what he does doesn't break up all and vice-versa. 因此,我需要确保他所做的并不会全部分解,反之亦然。

I'd like to introduce a CI server too, but this will be the topic of other questions. 我也想介绍一个CI服务器,但这将成为其他问题的主题。 Now the question is: I'm currently reading "The Art Of Unit Testing" (that's a suggested masterpiece!) and what the author underlines is that Unit Testing is different from Integration Testing. 现在的问题是:我正在阅读“单元测试的艺术”(这是一个建议的杰作!),作者强调的是单元测试与集成测试不同。 That's clear for me and, if I understood well, Business Logic unit testing should avoid to be dependent on database connections and so on. 这对我来说很清楚,如果我理解的话,Business Logic单元测试应该避免依赖于数据库连接等等。 First of all: am I right? 首先:我是对的吗?

So, supposing that I'm right (ie when I unit test my BLL I should stub the database), how will I do it? 所以,假设我是对的(即当我单元测试我的BLL时我应该存根数据库),我该怎么做? I've read that there are some framework for db mocking. 我读过有一些db mock的框架。 Should I use one of these? 我应该使用其中一种吗? Which do you use? 你用哪个?

Next question: do you really think this is a correct way to do? 下一个问题:你真的认为这是一种正确的方法吗? I mean: in my project the BL interfaces with database through Entity Framework. 我的意思是:在我的项目中,BL通过Entity Framework与数据库连接。 So, for example, when the method "UpdateItem" in my BLL is called, it does something and then saves the ObjectContext. 因此,例如,当我的BLL中的方法“UpdateItem”被调用时,它会执行某些操作然后保存ObjectContext。 This ObjectContext is the Entity Framework dependency I need to remove in my BL. 这个ObjectContext是我需要在BL中删除的Entity Framework依赖项。 But what should I test in such a method? 但是我应该用这种方法测试什么呢? I really can't understand unit testing the BL layer without testing the DAL together... Can you give me some example? 我真的无法理解单元测试BL层而没有一起测试DAL ......你能给我一些例子吗?

Thanks a lot for your efforts! 非常感谢您的努力!

Marco 马尔科

Yes, 是,

Business Logic unit testing should avoid to be dependent on database 业务逻辑单元测试应避免依赖于数据库

You are right about that. 你是对的。

I recommend that you: 我建议你:

  • use a suite of unit tests for the Business Layer, using stubs instead of real DB calls. 使用一组单元测试用于业务层,使用存根而不是真正的数据库调用。 You can stub the DB with whatever suits you best (you own fake classes or mocking libraries), provided that you have abstractions over the DB components. 您可以使用最适合您的数据(您拥有假类或模拟库)来存储数据库,前提是您对数据库组件进行了抽象。
  • use a suite of Integration tests to test the actual DB calls (and only that!) 使用一套集成测试来测试实际的数据库调用(仅限于此!)

The main differences between unit tests and integration tests are: * unit tests are fast and don't need any configuration * integration tests may be slow and require proper configuration (a database should be set up and there should be a proper connection string pointing to it). 单元测试和集成测试之间的主要区别是:*单元测试很快,不需要任何配置*集成测试可能很慢并且需要正确配置(应该设置数据库并且应该有适当的连接字符串指向它)。

I think this is good because it allows you to run the business unit tests very often as you make changes to your code. 我认为这很好,因为它允许您在更改代码时经常运行业务单元测试。 This is important, because you get very fast feedback (usually within 1-2 seconds) that the changes you made didn't break anything. 这很重要,因为您获得非常快速的反馈(通常在1-2秒内),您所做的更改不会破坏任何内容。

Once in a while, you can run the integration tests (that will take longer) to validate that the DB still works correctly. 偶尔,您可以运行集成测试(需要更长时间)来验证数据库是否仍能正常工作。

Also, I suggest you read the book that you mentioned. 另外,我建议你阅读你提到的那本书。 It think it is a very important source of information on this topic. 它认为这是有关该主题的非常重要的信息来源。

Stubbing the database is a huge task, that I don't think is worth it. 对数据库进行存根是一项艰巨的任务,我认为这不值得。 I prefer having a special database copy, with carefully prepared data that is suitable the unit tests. 我更喜欢有一个特殊的数据库副本,精心准备的数据适合单元测试。

The tests can be run within a transaction that is rolled back at the end of the test. 测试可以在测试结束时回滚的事务中运行。 That way, the unit test database is never modified by the tests and always kept in a known state. 这样,单元测试数据库永远不会被测试修改并始终保持已知状态。

I wrote about it in Using Transactions for Unit Tests on my blog. 我在博客上使用单元测试交易时写了这篇文章。 The sample code there is for linq-to-sql, but the same approach works for entity framework as well. 示例代码用于linq-to-sql,但同样的方法也适用于实体框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM