简体   繁体   中英

Unable to test controller action in rspec

I am trying to test a controller action on a non-restful route.

config/routes.rb :

  match '/integration/:provider/callback' => "providers#manual_auth", as: :integration_callback

You can see that also via rake routes :

integration_callback /integration/:provider/callback(.:format) providers#manual_auth

In my spec file:

spec/controllers/providers_controller_spec.rb :

describe ProvidersController do
  describe '#manual_auth' do
    it 'hits the manual_auth action' do
      get :manual_auth, use_route: :integration_callback
    end
  end
end

That gives me an error of:

Failures:

  1) ProvidersController#manual_auth hits the manual_auth action
     Failure/Error: get :manual_auth, use_route: :integration_callback
     ActionController::RoutingError:
       No route matches {:controller=>"providers", :action=>"manual_auth"}

However in app/controllers/providers_controller.rb I have

class ProvidersController < ApplicationController
  def manual_auth
    logger.info "Got into manual auth"
  end
end

I should mention I'm purposely avoiding a request spec here because I need to be able to access and set a session object(that lives in this #manual_auth action) which apparently can only be done in controller tests, not request specs.

The integration_callback has one parameter, which is :provider .

Try this:

get :manual_auth, provider: 'test', use_route: :integration_callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM