Capybara等待ajax而不使用睡眠

时间:2014-04-08 05:34:56

标签: ruby-on-rails angularjs rspec capybara ngtable

我正在使用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,需要一瞬间更新,没有睡眠就会失败。 Capybara过去常常使用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()))
  })

1 个答案:

答案 0 :(得分:0)

尝试将Capybara.default_wait_time最多撞击3秒或4。

如果失败,请尝试更改规范以查找闪存通知消息,然后再检查该项是否已从页面中删除。 (假设flash消息在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

修改 - 删除了解释,因为它不正确。