BroiSatse
BroiSatse

Reputation: 44675

Rspec and reset_session do not work together

I am having some troubles with one rspec test. I am writing simple logging-in system. Everything works fine, except for the logging-out test:

context 'when user is logged in' do
  before(:each) do
    session[:user_id] = User.first.id;
  end

  it 'allows user to log out' do
    delete :destroy
    session.should be_empty
  end
end

Destroy action:

def destroy   
  reset_session
  redirect_to new_session_path
end

I have checked that during the test session is cleared right after reset_session command, however it is not cleared in rspec after delete :destroy. I am wondering how those two sessions are correlated.

I have also tried session.delete(:user_id) with exactly same result. It seems that rspec detects adding keys to session without any problems, but cannot see sth was removed. Ideas?

Upvotes: 1

Views: 4074

Answers (2)

Peter Alfvin
Peter Alfvin

Reputation: 29389

In my tests, reset_session does clear the session variable as perceived by RSpec based on tests before and after the delete :destroy call. I did have to remove a notice: on a following redirect in my controller, however, which otherwise populated the session variable with the notice information.

If you haven't already printed out the value of session within the controller before and after your redirect, I would do that.

Upvotes: 0

Mohamed Yakout
Mohamed Yakout

Reputation: 3036

Try this test:

it 'allows user to log out' do
  delete :destroy
  request.original_url.eq new_session_path
end

This link may help you, get current url

Upvotes: 0

Related Questions