Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Capybara discarding my session after one event?

Testing a rails application which has before_filter :authenticate_user! for most controllers, I cannot get Capybara to preserve a session.

I have Capybara configured using PhantomJS with poltergeist.

I use the following helper:

require 'spec_helper'
include Warden::Test::Helpers

module FeatureHelpers
  def login(user = FactoryGirl.create(:default_user))
    login_as user, scope: :user
    user
  end
end

I have the following spec file:

require 'spec_helper'
include Warden::Test::Helpers

feature 'Leads Data Tasks View' do
  before(:each) do
    @user = login
  end

  after{ Warden.test_reset! }

  context "clicking a task button" do
    scenario "login persists across multuple actions", js: true do
      visit '/tasks'

      page.should have_selector('#parse', count: 1)
    end
  end
end

When I run the test as it's shown here, it will pass. However, if I invoke a click_link on something that performs AJAX actions, or if I simply try to do visit '/tasks' twice, the should assertion will fail because I'll get redirected to the login page of the app.

I've tried a few different approaches, including setting up a Capybara::Session, but I still get 401 codes on AJAX requests and I can only successfully visit once per spec.

What am I doing wrong?

like image 832
asfallows Avatar asked Sep 04 '13 20:09

asfallows


People also ask

What does capybara do in Rails?

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in.

What is Rspec capybara?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language. Capybara.


1 Answers

So the problem was that phantomjs driver (poltergeist) was using a separated database connection. I had the exact same issue before and I got a solution from railscast episode 391 with the code in spec/support/shared_db_connection.rb

Since I can't find license for his code so I will just link to the code here

like image 123
j03w Avatar answered Sep 22 '22 19:09

j03w