无法在 rspec 中测试控制器操作
Unable to test controller action in rspec
我正在尝试在非 restful 路线上测试控制器操作。
config/routes.rb
:
match '/integration/:provider/callback' => "providers#manual_auth", as: :integration_callback
你也可以通过 rake routes
:
看到
integration_callback /integration/:provider/callback(.:format) providers#manual_auth
在我的规范文件中:
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
这给我一个错误:
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"}
但是在app/controllers/providers_controller.rb
我有
class ProvidersController < ApplicationController
def manual_auth
logger.info "Got into manual auth"
end
end
我应该提一下,我在这里故意避免请求规范,因为我需要能够访问和设置会话对象(存在于此 #manual_auth
操作中),这显然只能在控制器中完成测试,而不是请求规格。
integration_callback
有一个参数,即:provider
。
试试这个:
get :manual_auth, provider: 'test', use_route: :integration_callback
我正在尝试在非 restful 路线上测试控制器操作。
config/routes.rb
:
match '/integration/:provider/callback' => "providers#manual_auth", as: :integration_callback
你也可以通过 rake routes
:
integration_callback /integration/:provider/callback(.:format) providers#manual_auth
在我的规范文件中:
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
这给我一个错误:
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"}
但是在app/controllers/providers_controller.rb
我有
class ProvidersController < ApplicationController
def manual_auth
logger.info "Got into manual auth"
end
end
我应该提一下,我在这里故意避免请求规范,因为我需要能够访问和设置会话对象(存在于此 #manual_auth
操作中),这显然只能在控制器中完成测试,而不是请求规格。
integration_callback
有一个参数,即:provider
。
试试这个:
get :manual_auth, provider: 'test', use_route: :integration_callback