Why is spec failing?

103 Views Asked by At

I have two specs. One written using all the defaults (I think selenium is the default?) and the other using webkit. They are exactly the same, but one fails the other doesn't

Spec

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

The javascript in the spec above happens on the click_link 'Add Payment Scheme'. It adds the controls for a payment scheme to the page and these are filled in. Note I know the IDs look strange, but when you use nested attributes and add controls dynamically to the page the ID has a random part in it (usually calculated of the date). So I can't use a direct ID. I have to use a funky css selector.

Results for Default

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

Results for 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

When I save and open the page using launchy, I see that for some reason it's not filling in the payment scheme fields. This functionality works fine using selenium. I have tried reworking the test, but no luck there.

I think that is everything. Any input would be appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

It could be a couple of things

The most obvious is that you could be having async problems because you're using Capybara. The Capybara/test thread tells the browser to click the button and then continues on. It doesn't wait for anything else to happen, so the browser could still be working on the JS and sending out a request back to the application for example. You could solve this with a sleep or with some trigger to listen for.

It could also be that your JS for the button click or the code for handling the JS request is just not doing what it should be.