Search code examples
ruby-on-railsrspectddcapybarabdd

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.


Solution

  • 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!