Capybara webkit 的数据库清理器问题 [英] Database Cleaner issue with Capybara webkit

查看:7
本文介绍了Capybara webkit 的数据库清理器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Cucumber 编写我的集成测试,并使用 Database Cleaner 来保持我的数据库清洁.一切都很完美,因为我的测试不需要 Javascript.

I am using Cucumber to write my integration tests and Database Cleaner to keep my db clean. Everything perfectly works as my tests don't require Javascript.

我可以使用 Capybara webkit 使这些最后的测试通过,但是我的数据库没有被清理完全没有.

I can make these last tests pass using Capybara webkit, but then my db is not cleaned at all.

这是我的 features/support/env.rb 文件:

require 'simplecov'
SimpleCov.start 'rails'
require 'cucumber/rails'

Capybara.default_selector = :css
Capybara.javascript_driver = :webkit

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner[:active_record].strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end


Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end

我尝试了类似于 this 检查 Capybara 使用了哪个驱动程序,但它不起作用.我还尝试了 这篇文章但没有任何效果...

I tried something similar to this to check which driver is used by Capybara but it didn't work. I also tried the hack mentioned in the third part of this post but then nothing worked at all...

我真的不知道如何做到这一点,任何帮助将不胜感激.

I really don't know how to achieve this and any help would be greatly appreciated.

提前致谢.

推荐答案

快速解答:

将您的 JavaScript 测试配置为使用截断而不是事务:

Configure your JavaScript tests to use truncation instead of transactions:

DatabaseCleaner.strategy = :truncation

更长的解释:

事务策略不适用于 JavaScript 测试,因为大多数支持 JavaScript 的水豚驱动程序在与应用程序代码不同的线程中运行测试.

The transaction strategy doesn't work well with JavaScript tests, because most JavaScript-capable capybara drivers run the tests in a different thread than the application code.

这是该过程的基本概述:

Here's a basic outline of the process:

  • Capybara 使用 webrick 或 Thin 在后台线程中启动您的机架应用程序.
  • 主线程设置驱动程序,提供运行机架应用程序的端口.
  • 您的测试要求驱动程序与应用程序交互,这会导致虚假网络浏览器对您的应用程序执行请求.

这是必要的,因为很难制作一个针对内存 Rack 应用程序执行请求的假浏览器.在某些数据库驱动程序中,从多个线程对同一事务执行查询是不安全的.

This is necessary because it's difficult to make a fake browser that performs requests against an in-memory Rack application. In some database drivers, it isn't safe to perform queries from multiple threads against the same transaction.

这样做的最终结果是您需要在测试代码中提交事务,以便数据在应用程序代码中可见.解决此问题的最简单方法是使用截断数据库清理器策略.

The end result of this is that you need to commit transactions in your test code in order for the data to be visible in your application code. The easiest way to fix this is to use the truncation database cleaner strategy.

您可以配置 RSpec(或 Cucumber)以将事务用于除 JavaScript 测试之外的所有内容.这对于非 JavaScript 测试会更快,同时仍然适用于 JavaScript 测试.

You can configure RSpec (or Cucumber) to use transactions for everything but JavaScript tests. This will be faster for non-JavaScript tests while still working for JavaScript tests.

Avdi Grimm 有一篇关于这个主题的好博文详细描述了解决方案:http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

Avdi Grimm has a good blog post on this subject that describes the solution in detail: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

这篇关于Capybara webkit 的数据库清理器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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