B Seven
B Seven

Reputation: 45941

Multiple sessions with Rspec and Capybara?

Trying to use a session, but getting strange results.

The following RSpec code seems to be generating 2 separate sessions:

visit start_path
post session_path, { foo: "bar" }

In the method called by the start_path controller:

session[:started] = 'yes' puts "Start: #{session.inspect}"

In the sessions controller:

def create
  session[:foo] = params[:foo]
  head :created
  puts "Sessions controller: #{session.inspect}"
end

The output looks like:

Start: {"session_id"=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ...
Sessions controller: {"session_id"=>"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" ...

The session ID's are different.

Is this because RSpec is creating 2 sessions, the post call creates a separate session, or my app is doing something to separate the sessions?

Why are there 2 sessions being created?

Tried changing create to another name, but that did not make a difference.

Using cookie store.

Upvotes: 1

Views: 1494

Answers (2)

B Seven
B Seven

Reputation: 45941

Thanks to Stack Overflow, found the solution here: Rails - Losing session with Integration Tests and Capybara - CSRF related?

The problem is that Capybara has its own session.

Need to use page.driver.post instead of post!

Upvotes: 0

Unkas
Unkas

Reputation: 3708

In modern Capybara versions you can do it the simple way:

  it 'should testing' do
    login user1
    # do stuff

    Capybara.using_session :user2 do
      login user2
      # do something in another chrome window as user2
    end
  end

Upvotes: 1

Related Questions