简体   繁体   English

无法在RSpec中测试控制器动作

[英]Unable to test controller action in rspec

I am trying to test a controller action on a non-restful route. 我正在尝试在非静态路由上测试控制器操作。

config/routes.rb : config/routes.rb

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

You can see that also via rake routes : 您也可以通过rake routes看到:

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

In my spec file: 在我的规格文件中:

spec/controllers/providers_controller_spec.rb : 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 但是在app/controllers/providers_controller.rb我有

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. 我应该提一下,我故意在这里避开一个请求规范,因为我需要能够访问和设置一个会话对象(位于#manual_auth操作中),该对象显然只能在控制器测试中完成,而不能在请求规范中完成。

The integration_callback has one parameter, which is :provider . integration_callback具有一个参数,即:provider

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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