Capybara:js => true导致测试失败 [英] Capybara with :js => true causes test to fail
问题描述
我是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阅读了Capybara自述文件.com / jnicklas / capybara 它解决了我的问题。
I've read the Capybara readme at https://github.com/jnicklas/capybara and it solved my issue.
事务性灯具仅适用于默认的Rack :: Test驱动程序,但
不适用于像Selenium这样的其他驱动程序。 Cucumber会自动处理这个
,但是使用Test :: Unit或RSpec,你可能需要使用
database_cleaner gem。请参阅此说明(以及解决方案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拥有自己的线程问题运行非机架驱动程序时的线程,使事务夹具功能在另一个上下文中使用第二个连接。因此驱动程序线程永远不会在运行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
这篇关于Capybara:js => true导致测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!