简体   繁体   English

我是否应该对我的SQLRepository运行单元测试?

[英]Should I run Unit Tests against my SQLRepository as well?

I'm developing a Domain Model based on the Repository Pattern and all of my Unit Tests as part of TDD are going against a Test Repository. 我正在开发一个基于存储库模式的域模型,作为TDD的一部分,我的所有单元测试都是针对测试存储库的。

My question is: at what point do I create integration tests against the SQL version of the Repository? 我的问题是:我在什么时候针对存储库的SQL版本创建集成测试?

My concern is that the code to access data from objects (Test Repository) will work fine. 我担心的是从对象(Test Repository)访问数据的代码可以正常工作。 But the database version (SQL Repository) is so different under the covers that my vital code in the SQL Repository will end up not working and be in itself untested. 但是数据库版本(SQL Repository)的内容是如此不同,以至于我的SQL资源库中的重要代码最终无法正常运行并且本身未经过测试。 How do I ensure that it's working as intended? 我如何确保它按预期工作? Am I missing something about the process? 我错过了一些关于这个过程的东西吗

Regards. 问候。

You should have tests which mock out the repositories (as it looks like you do), that doesn't query the database itself, but return results as if they did. 您应该拥有模拟存储库的测试(就像您看到的那样),它不会查询数据库本身,而是返回结果,就像它们那样。 These are the tests for functions that call the repository functions. 这些是调用存储库函数的函数的测试。

But it is also useful and recommended to have tests that check the database itself, and check if they return what they should. 但它也是有用的,建议用于检查数据库本身的测试,并检查它们是否返回它们应该的内容。 They should be also "unit tests", not depending on other stuff. 它们也应该是“单元测试”,而不是取决于其他东西。 Try not to depend that the database is on a determined state, but instead, make a setup to build your database initial state.They will probably be slower, and may not be run on every commit and build (I mean, don't run if it really takes a lot of time). 尽量不要依赖数据库处于确定状态,而是进行设置以构建数据库初始状态。它们可能会更慢,并且可能不会在每次提交和构建时运行(我的意思是,不要运行如果真的需要很多时间)。

Finally, in your integration tests, do all the stuff you should do. 最后,在集成测试中,做你应该做的所有事情。

In my opinion, as soon as you have the corresponding database code that your unit under test depends on go ahead and also add integration tests that use the real repository. 在我看来,只要你有相应的数据库代码,你的被测单元依赖于继续,并添加使用真实存储库的集成测试。 Faking and stubbing things out make unit testing easier and allows you to concentrate on a particular aspect of your code without getting frustrated by external dependencies and setting them up correctly. 伪造和剔除事物使单元测试更容易,并使您可以专注于代码的特定方面,而不会受到外部依赖性的挫败并正确设置它们。 However, at the end of the day, you're not shipping products with stubs, fakes, and mocks. 但是,在一天结束时,您不会发送带有存根,假货和嘲笑的产品。 The shipped product has real dependencies and components that all must work together. 随附的产品具有真正的依赖关系和组件,所有这些都必须协同工作。 Therefore, whenever you run your tests, you need to know if parts of your applications are working together or not. 因此,无论何时运行测试,您都需要知道应用程序的某些部分是否在一起工作。 If a part of my application is not able to persist changes to the database, I would like to know it as soon as possible. 如果我的应用程序的一部分无法持久更改数据库,我想尽快知道它。 So if your external dependency, database in your case, is at a level where it is providing some level of functionality and value to your code, go ahead and add a suite of integration tests. 因此,如果您的外部依赖项(在您的情况下是数据库)处于为代码提供某种级别的功能和价值的级别,请继续添加一组集成测试。

Note that running integration tests will usually take longer than unit tests. 请注意,运行集成测试通常需要比单元测试更长的时间。 So you may want to run just the unit tests during your CI builds. 因此,您可能希望在CI构建期间仅运行单元测试。 That way, you get a feedback on the health of your build and codebase quicker. 这样,您可以更快地获得有关构建和代码库运行状况的反馈。 However, you should include integration tests in your nightly build so that you know how your code is working in reality. 但是,您应该在每晚构建中包含集成测试,以便了解代码在实际中的工作方式。

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

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