Runtimeerror: Actioncontroller::Rackdelegation in Rspec 2.10.1 for Rails 3.1.4 Application Controller

Rspec: Test redirects in Devise::OmniauthCallbacksController subclass

You will need to do three things to get this accomplished.

  • enter OmniAuth test environment
  • create an OmniAuth test mock
  • stub out your from_omniauth method to return a user

Here is a possible solution, entered in the spec itself
(spec/feature/login_spec.rb for example) . . .

let(:current_user) { FactoryGirl.create(:user) }

before do
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
provider: :facebook,
uid:'12345',
info: {
name: "Joe"
}
})
User.stub(:from_omniauth).and_return(current_user)
end

I adapted this from a google authentication, so facebook may require more fields, but those are the only ones required by omniauth docs. You should be able to find the correct fields by looking at your database schema and finding fields that match the documentation.

In my case, the minimum was enough to pass the request phase and move onto the stubbed out method returning my user.

This example also uses FactoryGirl.

It may not be perfect, but I hope it helps. Good luck!

-Dan

Getting a delegation error in the API controller test module in Rspec

If anyone is working on a similar issue writing controller specs, here is how I solved this based on these 2 guides: http://codegur.com/22603728/test-user-authentication-with-rspec and https://gayleforce.wordpress.com/2012/12/01/testing-rails-before_filter-method/.

describe Api::ApiController, type: :controller do
describe '#validate_api_request' do
controller(Api::ApiController) do
before_filter :validate_api_request
def fake
render text: 'TESTME'
end
end

before do
routes.draw { get 'fake', to: 'api/api#fake' }
end

it 'verified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(true)
expect(subject.send(:validate_api_request)).to be_nil
end

it 'unverified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(false)
get 'fake'
expect(response.status).to be(401)
end
end
end

Test User Authentication with RSpec

Figured out. The point is - response should not be nil since action answers something (renders and not nil - empty string). And render status: :unauthorized wrong too, there is no render in spec, it's a line from controller (i wonder how it appears there). So the right spec for this method:

context "when user not logged in" do
controller(ApplicationController) do
before_action :authenticate_user!
def custom
render text: 'response'
end
end

before do
subject.send(:current_user=, nil)
routes.draw { get 'custom' => 'anonymous#custom' }
get 'custom'
end

it { should respond_with :unauthorized }
it "returns nothing" do
expect(response.body).to be_blank
end
end


Related Topics



Leave a reply



Submit