In my rails application i am using Devise
for user authentication.
When a user logs in to the application, Devise
authenticates that user and creates a session for that particular user.
I need to test that, when a user logs in, a session is created for that user. How do i do that?
In my acceptance test, If i do something like ,
feature 'User logs in' do
scenario 'with valid email and password' do
log_in_with 'valid@example.com', 'password'
expect(page).to have_content('Log out')
end
def log_in_with(email, password)
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Log in'
end
end
Still i am not checking that a new session was created for the logged in user. This does not fulfill my requirement as i am not checking if an actual user session was created.
Now, if i use warden
instead:
user = FactoryGirl.create(:user)
login_as(user, :scope => :user)
...
i am just saying that login as so and so user and go to next steps like maybe check for a Log Out link or an Edit Profile link, but no check on session creation for the user.
If i do this in controller specs, where i have access to sign_in
and sign_out
methods :
describe Devise::SessionsController do
it "user signed in successfully" do
logged_user = User.create(:email => "valid@example.com", :password => "12345678")
sign_in logged_user
expect(controller.user_session).not_to be nil
end
end
Here, i have access to user_session
, current_user
, user_signed_in?
methods.
Out of these 3 choices, i opted for user_session
as it seems to be associated with current user session.
Is this the correct approach?
Or is there another alternative or direct method available to check that a session is active or was created for a user?
Is there a way to check this scenario in my acceptance test?