Reputation: 4049
I'm trying to visit '/requests/new
. Now in the controller code, I've written it so that if a session doesn't exist, the user is redirected back to the home page. like so:
def new
@pagetitle = "What would you like to borrow?"
if session[:signup_email].nil?
flash[:danger] = "Please enter your email to get started"
redirect_to root_path
else
@signup_parent = Signup.find_by_email(session[:signup_email].downcase)
if @signup_parent.tos != true || @signup_parent.streetone.blank? || @signup_parent.streettwo.blank? || @signup_parent.zipcode.blank?
flash[:danger] = "Almost there! We just need a little more info"
redirect_to edit_signup_path
else
@requestrecord = @signup_parent.requests.build
end
end
end
But the problem is that now in testing, I can't simulate a session. I tried to write:
before do
session[:signup_email] = Signup.find_by_id(@signup_ana.id)
visit '/requests/new'
save_and_open_page
fill_in '#borrow__1', :with => 1
click_button
end
But that threw an error: undefined local variable or method
session' for #`
Yet if I don't try to simulate it, the save_and_open_page
shows me that I just get re-routed back to home. How should I do this? Is there a way to stub/ mock this out??
Upvotes: 3
Views: 3811
Reputation: 41
you can try this gem rack_session_access Then you can call page.set_rack_session(:user_id => user.id) in Capybara
use allenwei solution and don't forget to add the following require on rails_helper (rails 4+) / spec_helper (rails 3+):
require "rack_session_access/capybara"
Upvotes: 0
Reputation: 984
For myself I found this two solutions:
The first solution, not to stub authentication process, seems more reasonable in integration(and request) specs as you test the integration of your system components. So I'd not hurt authentication feelings, it is a component too.
Upvotes: 0
Reputation: 4127
you can try this gem rack_session_access
Then you can call page.set_rack_session(:user_id => user.id)
in Capybara
Upvotes: 1