我有一个Cucumber功能,具有匹配的步骤定义,如下所示:
功能
Feature: Time reporting
@javascript
Scenario: User reports billable time with valid data
* I am signed in
* the project I worked on exists in the system
* I report 3 hours of work on the project
* 3 hours should be logged in the system
步骤:
Given(/^the project I worked on exists in the system$/) do
@company = FactoryGirl.create :company
@project = FactoryGirl.create :project, company: @company
end
When(/^I report (\d+) hours of work on the project$/) do |hours|
visit "/time-reporting/#{Date.today}"
select_from_chosen @company.name, from: "company_id"
select_from_chosen @project.name, from: "time_entry_project_id"
page.evaluate_script("document.querySelector('#time_entry_hours').value = #{hours}")
click_on "Report"
end
Then(/^(\d+) hours should be logged in the system$/) do |hours|
#save_and_open_page
expect(TimeEntry.last.hours).to eq hours.to_f
end
此操作失败,因为即使我将TimeEntry
模型保存在TimeEntriesController
中,对TimeEntry.last
的调用也会失败。似乎模型没有坚持下去。
但是,如果我取消注释save_and_open_page
,那么断言是有效的,因为它现在已经神奇地找到了模型。
这是什么巫毒?
修改
我怀疑这与Capybara-Webkit有关。如果我以某种方式与页面交互,就像这样:
expect(page.has_content?("3.0 hours were logged.")).to be true
...我在它之后直接运行模型断言,然后两个断言都正常工作。
答案 0 :(得分:1)
我已经看过save_and_open_page用poltergeist和capybara-webkit修复测试。发生这种情况是因为save_and_open_page花了足够的时间让浏览器在测试检查完成之前完成它正在做的事情。解决方法是等待条件成立。示例:https://stackoverflow.com/a/23555725/634576
正如您所发现的那样,您还可以在页面上声明内容(capybara' s has_content
等待内容出现),并且从验收测试的角度来看,&#但是,无论如何都要比查看数据库更好。