Note that there are some explanatory texts on larger screens.

plurals
  1. POCookie related rspec test problem while working through tutorial
    primarykey
    data
    text
    <p>I've been working through Michael Hartl's <a href="http://ruby.railstutorial.org/ruby-on-rails-tutorial-book" rel="nofollow">Rails tutorial</a> (which is unbelievably awesome by the way).</p> <p>Anyway, everything has been going pretty well and I've nearly reached the end of chapter 10. The problem is that my rspec tests have started to generate some failures and I can't figure out what's wrong.</p> <p>The first failure occurred when I was working through <a href="http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec:the_destroy_action" rel="nofollow">the section on destroying users</a>. The test</p> <pre><code>before :each do @user = Factory :user end describe "as a non-signed-in user" do it "should deny access" do delete :destroy, :id =&gt; @user response.should redirect_to(signin_path) end end </code></pre> <p>gives the error:</p> <pre><code>UsersController DELETE 'destroy' as a non-signed-in user should deny access Failure/Error: delete :destroy, :id =&gt; @user NoMethodError: undefined method `admin?' for nil:NilClass # ./app/controllers/users_controller.rb:76:in `admin_user' # ./spec/controllers/users_controller_spec.rb:308:in `block (4 levels) in &lt;top (required)&gt;' </code></pre> <p>Here's the code the message references in users_controller:</p> <pre><code>def admin_user # the error tels me that current_user = NilClass redirect_to(root_path) unless current_user.admin? end </code></pre> <p>So I guess this would suggest that current_user isn't working correctly and is being set to nil. Now current_user involves a lot of methods of the SessionsHelper which (afaik) deal with setting the users ID in secure cookies and referencing the cookie as they move around the site. So this suggests that there is something wrong with the cookies. </p> <p>I've checked the browser and the cookie is being set, I've also gone over every part of the code and it all replicates the tutorial exactly as far as I can tell.</p> <p>Is there something else I should be looking at?</p> <p><b>Appendix</b></p> <p>Here is the contents of the SessionsHelper module:</p> <pre><code>module SessionsHelper def sign_in user # rails represents cookies as a hash and deals with the conversion for us # making the cookie "signed" makes it impervious to attack cookies.permanent.signed[:remember_token] = [user.id, user.salt] # this line calls the assignment operator below self.current_user = user end def current_user=(user) @current_user = user end # this is a getter method def current_user # this sets @current_user = to the user corresponding to the remember token # but only if @current user is undefined. ie it only works once @current_user ||= user_from_remember_token end def signed_in? # return true if current_user is not nil !current_user.nil? end def sign_out cookies.delete(:remember_token) self.current_user = nil end def current_user? user # returns true if the user object == the current_user object user == current_user end def authenticate deny_access unless signed_in? end def deny_access store_location # this is a shortcut for flash notices: flash[:notice] = "Please sign in to access this page." redirect_to signin_path, :notice =&gt; "Please sign in to access this page." end def redirect_back_or(default) redirect_to(session[:return_to] || default) clear_return_to end private def user_from_remember_token # the * allows us to give a 2 element array to a method expecting 2 seperate arguments User.authenticate_with_salt(*remember_token) end def remember_token # return [nil, nil] if the :remember_token cookie is nil cookies.signed[:remember_token] || [nil, nil] end def store_location # stores the url the browser was requesting session[:return_to] = request.fullpath end def clear_return_to session[:return_to] = nil end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload