rails 单元测试是否应该命中数据库? [英] Should rails units tests hit database or not?

查看:49
本文介绍了rails 单元测试是否应该命中数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的 rails 应用程序编写测试.我使用 TestUnit 进行单元测试和功能测试.我也使用黄瓜进行 GUI 测试.

I have been writing tests for my rails application. I use TestUnit for unit as well as functional tests. And I use cucumber for GUI testing as well.

但我发现 http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database 说单元测试最好不要命中数据库.

But I find that http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database says that it is better for unit tests not to hit the database.

我同意访问数据库需要相当长的时间.我已经使用 spork 来减少环境负载.

I do agree that hitting database takes considerable time. I already use spork to reduce environment loading.

测试 Rails 应用的最佳实践是什么?

What is the best practice when it comes to testing a rails app?

推荐答案

这是您不应该过于纠结名字的情况之一.Rails 将使用 ActiveRecord 模型的测试称为单元"测试,主要是因为它们是框架直接支持的最低级别的测试.根据测试知识,我承认我倾向于相当一致地分裂,单元测试应该没有被测单元外部的依赖关系;这意味着诸如持久性机制之类的东西,即数据库.

This is one of the cases where you shouldn't get too hung up on names. Rails refers to tests that exercise the ActiveRecord models as "unit" tests, largely because they're the lowest level tests directly supported by the framework. According to testing lore, to which I'll admit I tend to cleave fairly consistently, unit tests should have no dependencies external to the unit under test; this means things like persistence mechanisms, i.e. the database.

也就是说,在没有数据库的情况下测试任何复杂的 ActiveRecord 模型会很快让您想吃自己的手.Rails 假设您使用数据库进行测试;这只是框架的编写方式.您可以尝试排除所有内容,但最终会失败,因为您最终会弄乱 ActiveRecord 关联的内部结构(不适合胆小的人).您可以尝试将所有与持久性无关的代码提取到单独的模块中,以便在没有数据库的情况下进行测试,但是您将创建大量不必要的复杂性.

That said, testing ActiveRecord models of any complexity without the database will quickly make you want to eat your hands. Rails assumes that you use the database for tests; that's simply the way the framework is written. You can try to stub everything out, but you'll eventually fail since you'll end up messing around in the internals of ActiveRecord associations (not for the faint of heart). You can try to extract all of the non-persistence-related code into separate modules that test without the database, but you'll be creating quite a lot of unnecessary complexity.

不要试图对抗框架.ActiveRecord 和 ActionController 在生产和测试中都有非常具体的预期使用模式.毕竟,Rails 是关于约定的.如果你遵循使用模式,你会做更少的工作,也不会那么沮丧,而不是你为了坚持单元测试的理想而与约定作斗争.如果它让您感觉更好,请将您的 ActiveRecord 测试视为模型"测试,而将您的功能测试视为控制器"测试(这是 rspec 顺便使用的术语).

Don't try to fight the framework. ActiveRecord and ActionController have very specific patterns of expected use, both in production and in tests. Rails, after all, is all about convention. If you follow the pattern of use you will do much less work, and become much less frustrated, than if you fight the conventions in order to stick with unit testing ideals. If it makes you feel better, consider your ActiveRecord tests to be "model" tests, and your functional tests to be "controller" tests (this is the terminology that rspec uses, incidentally).

综上所述,您不应在模型测试(或规范)中不必要地访问数据库.某些验证(例如唯一性)需要数据库,但大多数不需要.许多与创建/保存回调和关联相关的操作都需要数据库,但其他操作则不需要.请注意您正在调用的操作正在执行的操作.

All that said, you shouldn't access the database unnecessarily in your model tests (or specs). Some validations, such as uniqueness, require the database, but most do not. Many actions related to create/save callbacks and associations require the database, but others do not. Be aware of what the actions you're invoking are doing.

这篇关于rails 单元测试是否应该命中数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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