为什么规格失败?

时间:2014-11-27 06:50:06

标签: ruby ruby-on-rails-4 rspec capybara capybara-webkit

我有两个规格。一个使用所有默认值编写(我认为selenium是默认值?),另一个使用webkit。它们是完全相同的,但一个失败,另一个没有

规格

let(:admin) {create(:user, :admin)}
  let(:programme) { create(:programme, :full_payment_scheme) }

  before(:each) do
    login_as(admin)
    visit edit_programme_path(programme)
  end

  scenario 'adding a payment_scheme', js: true do
      new_payment_scheme = create(:payment_scheme)
      visit edit_programme_path(programme)
      click_link 'Add Payment Scheme'
      select new_payment_scheme.name, from: all('select[id*="programme_programme_payment_schemes_attributes"][id*="payment_scheme_id"]').last[:id]
      fill_in all('input[id*="programme_programme_payment_schemes_attributes"][id*="markup"]').last[:id], with: 50
      fill_in all('input[id*="programme_payment_scheme_allocations_attributes"][id*="interval_weeks"]').last[:id], with: 10
      fill_in all('input[id*="programme_payment_scheme_allocations_attributes"][id*="allocation"]').last[:id], with: 100

      expect {click_button "Save"}.to change{ProgrammePaymentScheme.count}.by(1)
  end

上面规范中的javascript发生在click_link 'Add Payment Scheme'上。它将付款方案的控件添加到页面中并填写这些内容。注意我知道ID看起来很奇怪,但是当您使用嵌套属性并动态地向页面添加控件时,ID中有一个随机部分(通常是日期)。所以我不能使用直接ID。我必须使用一个时髦的CSS选择器。

默认结果

Programme edit page
  editing the payment_scheme
    adding a payment_scheme

Finished in 8.57 seconds (files took 1.25 seconds to load)
1 example, 0 failures

Top 1 slowest examples (7.6 seconds, 88.7% of total time):
  Programme edit page editing the payment_scheme adding a payment_scheme
    7.6 seconds ./spec/features/programmes/programme_edit_spec.rb:48
ryanme@Ryan-Mes-MacBook-Pro ~/Sites/phoenix ±20487304354485⚡ »  rspec spec/features/programmes/programme_edit_spec.rb

Webkit的结果

Programme edit page
  editing the payment_scheme
    adding a payment_scheme (FAILED - 1)

Failures:

  1) Programme edit page editing the payment_scheme adding a payment_scheme
     Failure/Error: expect {click_button "Save"}.to change{ProgrammePaymentScheme.count}.by(1)
       expected result to have changed by 1, but was changed by 0
     # ./spec/features/programmes/programme_edit_spec.rb:57:in `block (3 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 3.8 seconds (files took 0.53482 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/programmes/programme_edit_spec.rb:48 # Programme edit page editing the payment_scheme adding a payment_scheme

Top 1 slowest examples (3.48 seconds, 91.5% of total time):
  Programme edit page editing the payment_scheme adding a payment_scheme
    3.48 seconds ./spec/features/programmes/programme_edit_spec.rb:48

当我使用launchy保存并打开页面时,我发现由于某种原因,它没有填写付款方案字段。使用selenium可以正常使用此功能。我尝试过重新测试,但没有运气。

我认为这就是一切。任何意见都将不胜感激。

1 个答案:

答案 0 :(得分:1)

这可能是一些事情

最明显的是,您可能遇到异步问题,因为您正在使用Capybara。 Capybara /测试线程告诉浏览器单击按钮然后继续。它不会等待其他任何事情发生,因此浏览器仍然可以处理JS并将请求发送回应用程序。你可以通过睡眠或一些触发器来解决这个问题。

也可能是因为按钮点击的JS或处理JS请求的代码没有按照它应该做的那样。