我正在使用Capybara 2.x进行大型Rails/AngularJS应用程序的一些集成测试,我遇到了一个测试,我需要休眠才能让它工作。
我的测试:
describe "#delete", js: true do
it "deletes a costing" do
costing = Costing.make!
visit "/api#/costings"
page.should have_content("General")
click_link "Delete" # Automatically skips the confirm box when in capybara
sleep 0.4
page.should_not have_content("General")
end
end
它测试的代码使用的是 ng-table,它需要一瞬间才能更新,如果没有睡眠,它将失败。水豚曾经有过wait_until的方法,但它已经被淘汰了。我找到了这个网站:http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara 但无法获得任何推荐的替代方案来解决此问题。
这是我正在测试的代码。
# --------------------------------------------------------------------------------
# Delete
# --------------------------------------------------------------------------------
$scope.destroy = (id) ->
Costing.delete (id: id), (response) -> # Success
$scope.tableParams.reload()
flash("notice", "Costing deleted", 2000)
这将更新 ng-table(tableParams 变量),即此代码
$scope.tableParams = new ngTableParams({
page: 1,
count: 10,
sorting: {name: 'asc'}
},{
total: 0,
getData: ($defer, params) ->
Costing.query {}, (data) ->
# Once successfully returned from the server with my data process it.
params.total(data.length)
# Filter
filteredData = (if params.filter then $filter('filter')(data, params.filter()) else data)
# Sort
orderedData = (if params.sorting then $filter('orderBy')(filteredData, params.orderBy()) else data)
# Paginate
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
})
尝试将Capybara.default_wait_time
最多颠簸 3 秒或 4 秒。
如果失败,请尝试更改规范以查找闪光通知消息,然后再检查该项目是否已从页面中删除。(假设闪存消息在 HTML 正文中呈现)
describe "#delete", js: true do
it "deletes a costing" do
costing = Costing.make!
visit "/api#/costings"
page.should have_content("General")
click_link "Delete"
page.should have_content("Costing deleted")
page.should_not have_content("General")
end
end
编辑 - 删除了解释,因为它不正确。