RSpec &数据库清理器 - 在测试数据库中永久保留某些对象 [英] RSpec & Database Cleaner - Keep certain objects permanently in the test database

查看:26
本文介绍了RSpec &数据库清理器 - 在测试数据库中永久保留某些对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经(姗姗来迟)开始使用 数据库清理器在 RSpec/capybara 上测试我的 Rails 应用程序(一个购物网站) 清除数据库和 Factory Girl 为每个测试生成新对象(就像大多数人一样).这很好用,我认为在测试之间清除数据是个好主意.

I have (belatedly) started testing my Rails app (a shopping website) with RSpec/capybara, using database cleaner to clear the database and Factory Girl to generate new objects for every test (like most people do). This works fine, and I do think it is a good idea to clear the data between tests.

但是,生成同一对象的多个实例可能会变慢并且(据我所知)有点乏味.有些对象在我的数据库中始终相同,或者我将始终生成相同的副本以供测试.例如,我的 Package 模型定义了订阅包的定价和功能限制.它可能永远不会改变.

However, it can get slow and (as far as I can figure out) a bit tedious to generate multiple instances of the same object. There are some objects that are always the same in my database, or that I will always generate an identical copy of for testing. For example, my Package model, which defines the pricing and feature limits for a subscription package. It will probably never change.

有没有办法,通过这种配置(请评论并指定您是否需要更多信息),将对象的某些实例放入测试数据库中,并将它们从数据库清理器或任何其他排除如何在测试数据库中保留特定对象的永久副本?

Is there a way, with this configuration (please comment and specify if you require more info), to put certain instances of objects in the test database and exclude them from Database Cleaner, or any other way to keep permanent copies of specific objects in your test database?

这主要是为了提高测试速度.

This is mainly to increase testing speed.

推荐答案

如果您有应用程序永远不会更改的数据库对象,并且在您的生产和开发数据库以及测试数据库中都是相同的,那么正确的做法是要做的就是让它们成为种子.在 db/seeds.rb 中创建它们.更多关于种子的信息:http://guides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

If you have database objects that your application never changes, and that are the same in your production and development databases as well as in your test databases, the right thing to do is to make them seeds. Create them in db/seeds.rb. More on seeds here: http://guides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

如果您谈论的对象只属于您的测试数据库,您可以将它们设为 Rails 固定装置.更多关于装置的信息:http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures(但是请注意,fixtures 通常是一个坏主意,因为它们使您的测试更难阅读并鼓励您围绕现有的 fixtures 编写测试,这会导致到一团糟.测试的清晰度和健壮性比速度更重要.)

If the objects that you're talking about only belong in your test database, you could make them Rails fixtures. More on fixtures here: http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures (Beware, though, that fixtures are usually a bad idea, because they make your tests harder to read and encourage you to write tests around existing fixtures, which leads to an entangled mess. Test clarity and robustness is more important than speed.)

如果您正在使用 Database Cleaner 的截断或删除策略(可能是因为您在 Capybara 中使用了支持 Javascript 的驱动程序),并且您已经使用上述任一方法在测试之间将数据保留在您的测试数据库中,你可以告诉数据库清理器不要清空特定的表:

If you're using Database Cleaner's truncation or deletion strategy (probably because you're using a Javascript-capable driver with Capybara), and you've used either of the foregoing methods to leave data in your test database between tests, you can tell Database Cleaner to not empty specific tables:

DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}

DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}

(来源:https://github.com/bmabey/database_cleaner#how-to-use) 但是,我不知道有什么方法可以告诉 Database Cleaner 删除给定类的某些实例而不是其他实例.

(Source: https://github.com/bmabey/database_cleaner#how-to-use) I don't know of a way to tell Database Cleaner to delete some instances of a given class and not others, however.

这篇关于RSpec &数据库清理器 - 在测试数据库中永久保留某些对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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