title: Rspec tests with before_action and current_user (as in Hartl tutorial)
body: <p>I've just figured out a reason some of my tests continually failed, but the way I've fixed it doesn't seem right. I'd like to know whether this can be improved.</p>
  
  <p>I have a controller action, index, that looks like this.</p>
  
  <pre><code>class StuffController &lt; ApplicationController
      before_action :signed_in_user
  
      def index
          @stuff = current_user.stuff
      end
  end
  </code></pre>
  
  <p>The signed_in_user method, straight from the tutorial:</p>
  
  <pre><code>def signed_in_user
      unless signed_in?
          store_location
          redirect_to signin_url, notice: "Please sign in."
      end
  end
  </code></pre>
  
  <p>When I test the case that the /stuff endpoint is accessed without signing in, the test fails because:</p>
  
  <pre><code>Failure/Error: get "/stuff"
       NoMethodError:
         undefined method `stuff' for nil:NilClass
  </code></pre>
  
  <p>I know now that this is because, despite the before_action causing the response to redirect to signin_url, the index action is still executed in full. I changed the action to this to fix it:</p>
  
  <pre><code>def index
      @stuff = current_user.stuff unless current_user.nil?
  end
  </code></pre>
  
  <p>It seems like there ought to be a way to force the redirect to cease executing the rest of the code, rather than dirtying up each action with two checks for current_user.nil?. I did try adding <code>redirect_to signin_url, notice: "Please sign in." and return</code>, but that didn't work.</p>
  
  <p>Is this the right/rails way, or can it be improved?</p>