Why is Capybara discarding my session after one ev

2019-02-16 05:46发布

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?

1条回答
男人必须洒脱
2楼-- · 2019-02-16 06:41

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

查看更多
登录 后发表回答