簡體   English   中英

無法在RSpec中測試控制器動作

[英]Unable to test controller action in rspec

我正在嘗試在非靜態路由上測試控制器操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM