我应该费心对我的存储库层进行单元测试吗 [英] Should I bother unit testing my repository layer

查看:17
本文介绍了我应该费心对我的存储库层进行单元测试吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是把这个拿出来讨论一下.

Just putting this one out for debate really.

我进行了单元测试.有时感觉很费时间,但我完全是为了好处.

I get unit testing. Sometimes feels time consuming but I'm all for the benefits.

我使用 IoC 设置了一个包含存储库层和服务层的应用程序,并且我一直在对这些方法进行单元测试.

I've an application set up that contains a repository layer and a service layer, using IoC, and I've been unit testing the methods.

现在我知道隔离我的单元测试方法的好处,因此对其他方法的依赖很少或没有.

Now I know the benefits of isolating my methods for unit testing so there is little or no dependency on other methods.

我的问题是这个.如果我只通过我的服务层方法访问我的存储库方法,会测试服务层不够好?我正在针对测试数据库进行测试.

The question I've got is this. If I only ever access my repository methods through my service layer methods would only testing the service layers not be good enough? I'm testing against a test database.

难道它不能被认为是您只需要测试公共方法的想法的延伸吗?也许我只是想跳过一些测试;)

Could it not be considered an extension of the idea that you only need to test your public methods? Maybe I'm just trying to skip some testing ;)

推荐答案

是的,您应该测试您的存储库层.尽管这些测试中的大多数属于不同的测试分类.我通常将它们称为集成测试,以将它们与我的单元测试区分开来.不同之处在于对资源(您的数据库)有外部依赖性,并且这些测试可能需要更长时间才能运行.

Yes, you should test your repository layer. Although the majority of these tests fall into a different classification of tests. I usually refer to them as integration tests to distinguish them from my unit tests. The difference being that there is an external dependency on a resource (your database) and that these tests will likely take much longer to run.

单独测试您的存储库的主要原因是您将测试不同的内容.存储库负责处理翻译和与您使用的任何持久性存储的交互.另一方面,服务层负责将您的各种存储库和其他依赖项协调为表示业务逻辑的功能,这可能不仅仅涉及到存储库方法的中继,并且在某些情况下可能涉及对多个存储库的多次调用.

The primary reason for testing your repositories separately is that you'll be testing different things. The repository is responsible for handling translation and interaction with whatever persistence store you're using. The service layer, on the other hand, is responsible for coordinating your various respositories and other dependencies into functionality that represents business logic, which likely involves more than just a relay to a repository method and in some instances may involve multiple calls to multiple repositories.

首先,澄清服务层测试 - 在测试服务层时,应该模拟存储库,以便它们与您在服务层中测试的内容隔离.正如您在评论中指出的那样,这为您提供了更精细的测试级别并隔离了被测代码.您的单元测试现在也将运行得更快,因为没有数据库连接会减慢它们的速度.

First, to clarify the service layer testing - when testing the service layer, the repositories should be mocked so that they are isolated from what you're testing in the service layer. As you pointed out in your comment, this gives you a more granular level of testing and isolates the code under test. Your unit tests will also run much faster now because there are no database connections slowing them down.

现在,这里有一些将集成测试添加到存储库的优势...

Now, here are a few advantages of adding integration tests to your repositories...

  1. 它允许您在编写这些代码时对其进行测试,就像 TDD.
  2. 它确保您使用的任何持久性语言(SQL、HQL、序列化对象等)都针对您尝试执行的操作正确制定.
  3. 如果您使用对象关系映射器,它可以确保正确定义您的映射.
  4. 将来,您可能会发现需要支持另一种类型的持久化.根据存储库测试的结构方式,您可以重用大量测试来验证新数据库模式是否正常工作.对于实现数据库特定逻辑的存储库方法,显然您必须创建单独的测试.
  5. 与持续集成结合使用时,最好将存储库测试分开.集成测试本质上比单元测试需要更长的时间来运行.因此,它们通常以较低的间隔运行,以便从运行单元测试中获得的即时反馈不会延迟.

这些都是我在我参与的各种项目中看到的所有优势.可能还有更多.

Those are all advantages that I've seen in various projects that I've worked on. There may be more.

说了这么多,我承认我对存储库集成测试并不像对单元测试那么彻底.例如,在测试特定对象的更新时,我通常会满足于测试一个数据库列是否成功更新,而不是为每个单独的列创建单独的测试,或者创建一个更大的测试来验证一个测试中的每一列.对我来说,这取决于存储库方法正在执行的操作的复杂程度,以及是否有任何需要隔离的特殊条件.

All that having been said, I will admit that I'm not as thorough with the repository integration tests as I am with unit tests. When it comes to testing an update on a particular object, for example, I'm usually content testing that one database column was successfully updated rather than creating a separate test for each individual column or a larger test that verifies every column in one test. For me, it depends on the complexity of the operation that the respository method is performing and whether there's any special condition that needs to be isolated.

这篇关于我应该费心对我的存储库层进行单元测试吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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