svelandiag
svelandiag

Reputation: 4320

RSpec capybara: Tests with Ajax involved pass in isolation but fail when run together

I know other people have asked about this before, but I cannot find similar case to this issue.

I have two simple tests:

# Helper function
def select_file(fixture = 'basic.csv')
    visit '/import-contacts'
    fixture_path = "#{Rails.root}/spec/fixtures"
    attach_file('file', "#{fixture_path}/#{fixture}")
  end


it 'first test example' do
    select_file
    click_button 'Import'

    ['Name', 'Date of Birth', 'Phone', 'Address', 'Credit Card', 'Email'].each do |file_column|
      expect(page).to have_content(file_column)
    end
end

it 'second test example same instructions as above' do
    select_file
    click_button 'Import'

    ['Name', 'Date of Birth', 'Phone', 'Address', 'Credit Card', 'Email'].each do |file_column|
      expect(page).to have_content(file_column)
    end
end

Im just attaching a file to a file field and sending an ajax request to then check if some data got populated on a table. However Im not manipulating Databse data, Im not storing or trying to get any records from the data base, just a basic Ajax workflow that renders some data based on the file attached.

When I run all the test suite the first test passes, but the second one fails, please check that both tests have the same code, so I cannot find an explanation.

When I run the second test isolated it passes. Any idea what could be happening?

UPDATE Here are some extra details, the above is just an example, but the tests were different, I had to remove the second test and add the removed part to the first test and it passed.

The second test was replicating steps of first test, however at the end it was clicking a button that makes another AJAX call that renders a js.erb view file.

This was the error I was having at the beginning:

Failure/Error:
       respond_to do |format|
         format.js
       end
     
     ActionController::UnknownFormat:
       ActionController::UnknownFormat

However when I moved the asertions and steps of second test to the first test everything passed.

By the way not sure if has something to do with this issue but I set the webdriver at the beginning of the tests file

Capybara.current_driver = :apparition

if I remove the above line the tests fails due to they need to wait the resonse of the ajax request in order to pass.

Upvotes: 0

Views: 132

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

Since your error is about the format of the request, I'm guessing you're expecting all of your tests to run with the js enabled driver (selenium) but are only setting current_driver to that at the beginning of your tests. In the default Capybara setup, for system/feature specs, Capybara.current_driver gets reset to Capybara.default_driver at the end of every test and then if the test is tagged with js it gets set to the value of Capybara.javascript_driver. If you want all your system/feature specs to run with the same driver you can set Capybara.default_driver = :chrome (or whatever driver you're using) otherwise you need to tag the tests you want to run with that driver with the js metadata

it 'second test example same instructions as above', js: true do
  ...
end

Upvotes: 1

Related Questions