title: Test OAuth2 request in rspec test with oauth-plugin gem
body: <p>I use the latest oauth-plugin with Rails 3.1. I want to test my <strong>OAuth2</strong> API controllers with rspec tests. After trying a bunch of things to authorize my request I simply want to stub the <strong>oauthenticate</strong> filter to get rid of any authentication issues. But I still get a <em>401 Unauthorized</em> . Why??</p>
  
  <p>users_controller.rb:</p>
  
  <pre><code>class UsersController
    oauthenticate
  
    def update
      &lt;do something&gt;
    end
  end
  </code></pre>
  
  <p>users_controller_spec.rb:</p>
  
  <pre><code>describe UsersController do
    describe "POST 'update'" do
  
        before :each do
          controller.stub!(:oauthenticate).and_return true
        end
  
        it "should be a successful request" do
          post :update,  { "user_id" =&gt; "some id" }
          response.should be_ok
       end
  end
  </code></pre>
  
  <p>Expected ActionController::TestResponse to have a response code of 200, but got 401.</p>
  
  <p><a href="https://stackoverflow.com/questions/5268672/rspec-testing-for-oauth-provider">Rspec testing for oauth provider</a> doesn't help. With cucumber test everything works fine when setting a valid access token <em>Authorization header</em>.</p>