0

I'm working through the railstutorial.org book, chapter 9. When running the test suite, I can't get rid of the following failure. All the code has been attempted by first typing it in and then by copying and pasting from the book.

...............F............................................

Failures:

  1) SessionsController DELETE 'destroy' should sign a user out
     Failure/Error: controller.should_not be_signed_in
       expected signed_in? to return false, got true
     # ./spec/controllers/sessions_controller_spec.rb:69:in `block (3 levels) in <top (required)>'

Finished in 8.28 seconds
60 examples, 1 failure

Here's the code for the test in sessions_controller_spec.rb:

describe "DELETE 'destroy'" do

  it "should sign a user out" do
    test_sign_in(Factory(:user))
    delete :destroy
    controller.should_not be_signed_in
    response.should redirect_to(root_path)
  end
end

The (I think) relevant portions of sessions_controller.rb:

def destroy
  sign_out
  redirect_to root_path
end

The sign_out method is found in sessions_helper.rb

def current_user=(user)
  @current_user ||= user_from_remember_token
end

def current_user
  @current_user
end

def signed_in?
  !current_user.nil?
end

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

So, if I understand this correctly, the test, after signing in a factory user, is calling the SessionsController's destroy method, which calls sign_out (from SessionsHelper), which is explicitly setting self.current_user to nil. Then the test checks signed_in? with the line be_signed_in. Since the code sets self.current_user to nil, current_user.nil? should return true, !current_user.nil? should return false, which is what the test wants (controller.should_not be_signed_in).

Any help would be appreciated. I'm learning Ruby, Rails and TDD all at the same time, so I'm not sure where my problem lies.

3
  • Can you add the code for how you implement the current_user= function? Commented Feb 7, 2011 at 3:41
  • current_user=(user) returns @current_user ||= user_from_remember_token, and when looking at that I did find another mistake, as current_user was defined the same way, which checking the book, should return just @current_user. However, I changed that and I'm still getting the same error from the tests. Commented Feb 7, 2011 at 3:59
  • I apologize, signed_in? is found in sessions_helper.rb. I'll edit the question to reflect this and include the code for current_user= and current_user. Commented Feb 7, 2011 at 4:03

1 Answer 1

2

The problem is how you are setting current user - you are only ever setting it to login from the remember token, you aren't ever allowing it to be set to nil. You might want to try the following:

def current_user=(user)
  @current_user = user
end

def current_user
  @current_user ||= user_from_remember_token
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.