Capybara webkit的数据库清除程序问题 [英] Database Cleaner issue with Capybara webkit

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

问题描述

我使用Cucumber编写我的集成测试和数据库清理程序,以保持我的数据库清理。一切都很完美,因为我的测试不需要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.

我可以使用

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

这是我的功能/功能, 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 to检查哪个驱动程序被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的capybara驱动程序在与应用程序代码不同的线程中运行测试。

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启动您的机架应用程序或在后台线程中启动。 li>
  • 主线程设置驱动程序,提供运行机架应用程序的端口。

  • 您的测试要求驱动程序与应用程序交互,

这是必要的,因为很难创建一个假冒的浏览器来执行请求内存中的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测试以外的所有事务使用事务。

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 / configuration-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天全站免登陆