我有一个绿色测试:
test "study page & question screen" do
assert_title "Study | Word Up"
assert page.has_selector?('h3', text: "STUDY"), "Panel title missing or wrong"
assert page.has_selector?("div.progress-bar"), "Progress bar missing or with wrong class"
end
16:24:23 - INFO - Running: test/integration/study/study_test.rb
Started
StudyTest
test_answer_screen PASS (0.98s)
test_study_page_&_question_screen PASS (0.27s)
Finished in 1.26314s
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
因为我在同一页面上有很多(页面上)Javascript代码,我也想测试。因此,我告诉Capybara使用能够使用Javascript的驱动程序。例如webkit,但我也尝试了吵闹和硒。
test "study page & question screen" do
Capybara.javascript_driver = :webkit
Capybara.current_driver = Capybara.javascript_driver
assert_title "Study | Word Up"
assert page.has_selector?('h3', text: "STUDY"), "Panel title missing or wrong"
assert page.has_selector?("div.progress-bar"), "Progress bar missing or with wrong class"
end
突然,测试是红色的。虽然我没有做任何事情,只是改变了司机。
16:28:21 - INFO - Running: test/integration/study/study_test.rb
Started
StudyTest
test_answer_screen PASS (1.12s)
test_study_page_&_question_screen ERROR (5.11s)
Capybara::ExpectationNotMet: Capybara::ExpectationNotMet: expected "" to include "Study | Word Up"
这怎么可能?
答案 0 :(得分:0)
经过一些测试后,我认为您正在访问设置方法中的根路径。因此,您的测试访问根路径,然后更改Capybara驱动程序。尝试更改驱动程序,然后访问根路径。
基本上,添加
visit root_path
后
Capybara.javascript_driver = :webkit
Capybara.current_driver = Capybara.javascript_driver
这失败了:
setup do
visit root_path
end
test "study page & question screen" do
Capybara.javascript_driver = :webkit
Capybara.current_driver = Capybara.javascript_driver
assert_title "Study | Word Up"
end
通过:
setup do
end
test "study page & question screen" do
Capybara.javascript_driver = :webkit
Capybara.current_driver = Capybara.javascript_driver
visit root_path
assert_title "Study | Word Up"
end