Search code examples
ruby-on-railsrspecselenium-chromedrivercapybara

Capybara + RSpec fail in first test


I have a Rails Application with Devise gem. It is working with docker-compose.

Well, I create a simple test which expect when someone access some route without are logged, it going to redirect to /login path. Like this:

# frozen_string_literal: true

require "rails_helper"

RSpec.describe 'Home', type: :feature, js: true do
  context "when user is not logged" do
    5.times do |i|
      it "redirects to login page" do
        visit "/"
        puts i
        expect(page).to have_current_path("/login")
      end
    end
  end
end

That test is simple and works well, but sometimes that broke and I don't know why

  when user is not logged
Capybara starting Puma...
* Version 3.12.6 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:40303
    he will be redirected to login page (FAILED - 1)
1
    he will be redirected to login page
2
    he will be redirected to login page
3
    he will be redirected to login page
4
    he will be redirected to login page

But everytime when it's fail is the first interaction!


Solution

  • It's likely that your app is taking longer to startup/respond than the test is allowing. You can probably fix that by setting Capybara.default_max_wait_time to a higher value. It would be helpful if you changed your rspec output formatter to one that was more verbose so we can see the actual error that is causing the test to fail.