1

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?

BroiSatse
  • 44,031
  • 8
  • 61
  • 86

2 Answers2

1

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.

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • Awesome, I would never think that redirect_to is setting anything in a session, but it makes perfect sense. In my case the problem was turbolinks (and fact that I cached current_user in a controller). Thanks a lot! – BroiSatse Jan 04 '14 at 00:09
0

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

Community
  • 1
  • 1
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45
  • Thx, I got NameErrorundefined local variable or method `current_url'. Apart of that it is avoiding the problem, not solving it. – BroiSatse Jan 03 '14 at 21:46