Ruby on Rails Tutorial Test Not Passing
I'm working through the railstutorial.org b开发者_StackOverflowook, 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.
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
精彩评论