水豚与 :js =>true 导致测试失败 [英] Capybara with :js => true causes test to fail

查看:29
本文介绍了水豚与 :js =>true 导致测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Capybara 的新手,一般在 Rails 上测试过,所以如果这是一个简单的答案,请原谅我.

I'm new to Capybara and testing on Rails in general, so please forgive me if this is a simple answer.

我有这个测试

it "should be able to edit an assignment" do
    visit dashboard_path
    select(@project.client + " - " + @project.name, :from => "assignment_project_id")
    select(@team_member.first_name + " " + @team_member.last_name, :from => "assignment_person_id")
    click_button "Create assignment"
    page.should have_content(@team_member.first_name)
end

它按原样通过,但如果我添加 :js => true 它会失败

it passes as is, but if I add :js => true it fails with

cannot select option, no option with text 'Test client - Test project' in select box 'assignment_project_id'

我正在使用 FactoryGirl 创建数据,并且当测试在没有 JS 的情况下通过时,我知道该部分正在工作.

I'm using FactoryGirl to create the data, and as the test passes without JS, I know that part is working.

我已经尝试过使用默认的 JS 驱动程序和 :webkit 驱动程序(安装了 capybara-webkit)

I've tried with the default JS driver, and with the :webkit driver (with capybara-webkit installed)

我想我不太明白为 Capybara 开启 JS 是做什么的.

I guess I don't understand enough what turning on JS for Capybara is doing.

为什么打开 JS 测试会失败?

Why would the test fail with JS on?

推荐答案

我在 https://github.com 上阅读了 Capybara 自述文件/jnicklas/capybara 并解决了我的问题.

I've read the Capybara readme at https://github.com/jnicklas/capybara and it solved my issue.

事务性装置仅在默认的 Rack::Test 驱动程序中工作,但是不适用于 Selenium 等其他驱动程序.黄瓜照顾这个自动,但使用 Test::Unit 或 RSpec,您可能必须使用database_cleaner 宝石.请参阅此说明(以及解决方案 2解决方案 3) 了解详情.

Transactional fixtures only work in the default Rack::Test driver, but not for other drivers like Selenium. Cucumber takes care of this automatically, but with Test::Unit or RSpec, you may have to use the database_cleaner gem. See this explanation (and code for solution 2 and solution 3) for details.

但基本上它是一个线程问题,涉及 Capybara 在运行非 Rack 驱动程序时拥有自己的线程,这使得事务夹具功能在另一个上下文中使用第二个连接.因此驱动程序线程永远不会与正在运行的 rspec 处于同一上下文中.

But basically its a threading issue that involves Capybara having its own thread when running the non-Rack driver, that makes the transactional fixtures feature to use a second connection in another context. So the driver thread is never in the same context of the running rspec.

幸运的是,在要使用的 DatabaseCleaner 策略中进行动态切换可以轻松解决(至少对我来说已经解决了):

Luckily this can be easily solve (at least it solved for me) doing a dynamic switching in th DatabaseCleaner strategy to use:

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

这篇关于水豚与 :js =>true 导致测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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