BitOfUniverse
BitOfUniverse

Reputation: 6021

Cucumber and capybara scenario session

I did a lot of R&D and still have no answer for simple question - where is my session object?

I have a scenario like this:

Scenario: I should be able to add products to the basket
  Given I have a product named "Mousepad" # creates object using FactoryGirl
  When I add "Mousepad" to basket         # sends POST request to add product 
  Then I should have 1 item in my basket  # visit basket page

Here's basket method:

def set_current_basket
  @basket = (session[:basket] ||= Basket.new)
end 

It works fine in browser, but not in tests. @basket allways is new object. Even if I set session[:test] = 1 in the first step, it will be nil in the next one.

Did I miss something, how does it possible?

Upvotes: 3

Views: 946

Answers (1)

PedroSena
PedroSena

Reputation: 653

The idea behind Capybara is that you will emulate the user behavior, for that reason you shouldn't need direct session access.

However I agree that this is not true 100% of the time, specially when you rely on some third part software, in that case I recommend the gem rack_session_access, it will allow you to access you session like that:

page.set_rack_session basket: Basket.new

and access it by page.get_rack_session_key('basket')

Please keep in mind that this is a hack and you shouldn't need direct access to your session if your final user does not need it too.

Upvotes: 3

Related Questions