Rails 系统规范是否仍然需要 DatabaseCleaner? [英] Is DatabaseCleaner still necessary with Rails system specs?

查看:34
本文介绍了Rails 系统规范是否仍然需要 DatabaseCleaner?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我读过的有关 Rails 5.1 新系统规范的所有内容中,我的理解是 Rails 现在在内部处理数据库事务.

From all that I've read about Rails 5.1 new system specs my understanding was that Rails now handles database transactions internally.

来自 Rspec 的博客:"[以前] 你的测试和被测代码不能共享一个数据库事务,所以你不能使用 RSpec 的内置机制来回滚数据库更改,而是需要一个像数据库清理器这样的 gem.通过系统测试,Rails 团队已经做到了努力确保情况并非如此,因此您可以安全地使用 RSpec 的机制,而无需额外的 gem."

From Rspec's blog: "[previously] your tests and your code under test cannot share a database transaction, and so you cannot use RSpec's built in mechanism to roll back database changes, instead requiring a gem like database cleaner. With system tests, the Rails team has done the hard work to ensure that this is not the case, and so you can safely use RSpec's mechanism, without needing an extra gem."

我的经历不同:

  1. 升级到 Rails 后,我的 Rspec 功能测试全部通过5.1.
  2. 我将功能"规格重命名为系统"规格.所有测试都通过了.
  3. 我删除了 Database Cleaner gem,并删除了 rails_helper.rb 中的所有引用代码>.JS 测试现在由于 validates uniqueness 错误而失败.非 JS 测试通过.
  1. My Rspec feature tests were all passing after upgrading to Rails 5.1.
  2. I renamed the 'feature' specs to become 'system' specs. All tests passed.
  3. I removed the Database Cleaner gem, and removed all references from rails_helper.rb. JS tests now fail due to validates uniqueness errors. Non-JS tests pass.

我的测试非常简单.

let(:subject) { page }
let(:user) { create :user, name: "TestUser" }
it "displays the user page", :js do
  visit user_path(user)
  it is_expected.to have_content "TestUser"
end

禁用数据库清理器和 :js =>真 我得到名为TestUser 的用户已经存在.使用 :js =>false 测试通过.

With Database cleaner disabled and :js => true I get user named TestUser already exists. With :js => false the test passes.

系统测试和 rspec 目前的情况如何?Rails 是否在内部处理数据库事务,还是仍然需要 Database Cleaner?有没有人遇到过这个,或者可以指点我相关的信息?

What is the current situation with system tests and rspec? Does Rails handle database transactions internally, or is Database Cleaner still required? Has anyone encountered this, or can point me towards relevant info?

推荐答案

您不需要 DatabaseCleaner gem.以下是您需要执行的步骤的快速摘要:

You do not need the DatabaseCleaner gem. Here's a quick summary of steps you need:

  1. 移除 capybara-selenium gem 和 database_cleaner gem
  2. 添加selenium-webdriver gem
  3. 确保您使用的是 Rails 5.1.5 或更高版本.较早版本的 Rails 5.1 在 ActionDispatch::SystemTesting::Server 中存在导致问题的缺陷(已修复 此处).
  4. 在您的 rails_helper 文件中,设置 config.use_transactional_fixtures = true
  5. 如果您使用 Devise 进行身份验证,请在您的 rails_helper 文件中设置 config.include Devise::Test::IntegrationHelpers,键入::system
  6. 根据 诺埃尔·拉平.
  7. RSpec.feature 替换为 RSpec.describe.
  1. Remove capybara-selenium gem and database_cleaner gem
  2. Add selenium-webdriver gem
  3. Make sure you are using Rails 5.1.5 or later. Earlier versions of Rails 5.1 had a defect in ActionDispatch::SystemTesting::Server that caused problems (fixed here).
  4. In your rails_helper file, set config.use_transactional_fixtures = true
  5. If you are using Devise for auth, in your rails_helper file, set config.include Devise::Test::IntegrationHelpers, type: :system
  6. Set up a basic_configure file as per this post by Noel Rappin.
  7. Replace RSpec.feature throughout with RSpec.describe.

请参阅此提交,了解有关我如何管理交换机的更多详细信息.这很麻烦,但希望 5.1.5 解决了 ActionDispatch::SystemTesting::Server 问题后更容易(我不得不在 5.1.4 中修补文件,但现在您不必这样做).

See this commit for more details as to how I managed the switch. It was a hassle, but hopefully easier now that 5.1.5 has fixed the ActionDispatch::SystemTesting::Server issue (I had to monkey patch the file in 5.1.4, but you won't have to now).

这篇关于Rails 系统规范是否仍然需要 DatabaseCleaner?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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