<p>I've created a learning application using <a href="http://jimneath.org/2008/09/09/bort-base-rails-application/" rel="noreferrer">Bort</a>, which is a base app that includes Restful Authentication and RSpec.  I've got it up and running and added a new object that requires users to be logged in before they can do anything(<code>before_filter :login_required</code> in the controller).  [edit: I should also mention that the user <code>has_many</code> of the new class and only the user should be able to see it.]</p>

<p>I've created the new model/controller using Rspec's generators which have created a number of default tests.  They all pass if there is no <code>before_filter</code> but several fail, as should be expected, once the <code>before_filter</code> is in place.</p>

<p>How do I get the generated tests to run as if there is/is not a logged in user?  Do I need a whole batch of matching not logged in - redirect tests?  I assume it is some sort of mocking or fixture technique but I am new to RSpec and a bit adrift.  Good RSpec tutorial links would also be appreciated.</p>

[{'answer_id': 65748, 'author': 'TALlama', 'author_id': 5657, 'author_profile': 'https://Stackoverflow.com/users/5657', 'pm_score': 4, 'selected': True, 'text': '<p>I have a very similar setup, and below is the code I\'m currently using to test this stuff. In each of the <code>describe</code>s I put in:</p>\n\n<pre><code>it_should_behave_like "login-required object"\ndef attempt_access; do_post; end\n</code></pre>\n\n<p>If all you need is a login, or</p>\n\n<pre><code>it_should_behave_like "ownership-required object"\ndef login_as_object_owner; login_as @product.user; end\ndef attempt_access; do_put; end\ndef successful_ownership_access\n  response.should redirect_to(product_url(@product))\nend\n</code></pre>\n\n<p>If you need ownership. Obviously, the helper methods change (very little) with each turn, but this does most of the work for you. This is in my spec_helper.rb</p>\n\n<pre><code>shared_examples_for "login-required object" do\n  it "should not be able to access this without logging in" do\n    attempt_access\n\n    response.should_not be_success\n    respond_to do |format|\n      format.html { redirect_to(login_url) }\n      format.xml { response.status_code.should == 401 }\n    end\n  end\nend\n\nshared_examples_for "ownership-required object" do\n  it_should_behave_like "login-required object"\n\n  it "should not be able to access this without owning it" do\n    attempt_access\n\n    response.should_not be_success\n    respond_to do |format|\n      format.html { response.should be_redirect }\n      format.xml { response.status_code.should == 401 }\n    end\n  end\n\n  it "should be able to access this if you own it" do\n    login_as_object_owner\n    attempt_access\n\n    if respond_to?(:successful_ownership_access)\n      successful_ownership_access\n    else\n      response.should be_success\n    end\n  end\nend\n</code></pre>\n'}, {'answer_id': 71633, 'author': 'srboisvert', 'author_id': 6805, 'author_profile': 'https://Stackoverflow.com/users/6805', 'pm_score': 2, 'selected': False, 'text': '<p>I found a few answers to my own question.  Basically, I needed to understand how to mock out the user from <code>restful_authentication</code> so that the autogenerated rspec controller tests could pass even though I had added <code>before_filter: login_required</code>.</p>\n\n<p>Here are a few of my just found resources:</p>\n\n<p><a href="http://www.robbyonrails.com/articles/2008/08/19/rspec-it-should-behave-like" rel="nofollow noreferrer">RSpec: It Should Behave Like</a></p>\n\n<p><a href="http://matthewkwilliams.com/index.php/2008/08/19/rspec-restful_authentication-and-login_required/" rel="nofollow noreferrer">rspec, restful_authentication, and login_required</a></p>\n\n<p><a href="http://rubyforge.org/pipermail/rspec-users/2007-August/002834.html" rel="nofollow noreferrer">using restful_authentication current_user inside controller specs</a></p>\n\n<p><a href="http://edspencer.net/2008/08/drying-up-your-crud-controller-rspecs.html" rel="nofollow noreferrer">DRYing up your CRUD controller RSpecs</a> </p>\n'}, {'answer_id': 143976, 'author': 'mislav', 'author_id': 11687, 'author_profile': 'https://Stackoverflow.com/users/11687', 'pm_score': 1, 'selected': False, 'text': "<p>To mock a user being logged in, I hack into the controller to set <code>@current_user</code> manually:</p>\n\n<pre><code>module AuthHelper\n  protected\n\n  def login_as(model, id_or_attributes = {})\n    attributes = id_or_attributes.is_a?(Fixnum) ? {:id =&gt; id} : id_or_attributes\n    @current_user = stub_model(model, attributes)\n    target = controller rescue template\n    target.instance_variable_set '@current_user', @current_user\n\n    if block_given?\n      yield\n      target.instance_variable_set '@current_user', nil\n    end\n    return @current_user\n  end\n\n  def login_as_user(id_or_attributes = {}, &amp;block)\n    login_as(User, id_or_attributes, &amp;block)\n  end\nend\n</code></pre>\n"}, {'answer_id': 1943616, 'author': 'Marcos Oliveira', 'author_id': 127209, 'author_profile': 'https://Stackoverflow.com/users/127209', 'pm_score': 3, 'selected': False, 'text': "<p>When not testing the authentication but testing the controllers that needs the user to be authenticated I usually stub the filter method:</p>\n\n<pre><code>before(:each) do\n  controller.stub!(:authenticate).and_return(true)\nend\n</code></pre>\n\n<p>The above example works where my before_filter is set to the authenticate method:</p>\n\n<pre><code>before_filter :authenticate\n</code></pre>\n\n<p>and the authenticate in my app uses HTTP Basic Authentication, but it really can be any other authentication mechanism.</p>\n\n<pre><code>private\ndef authenticate\n  authenticate_or_request_with_http_basic do |user,password|\n    user == USER_NAME &amp;&amp; password == PASSWORD\n  end\nend\n</code></pre>\n\n<p>I think it's a pretty straightforward way to test.</p>\n"}]
['https://Stackoverflow.com/questions/64827', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6805/']