2012-12-30 55 views
2

我在爲我的Omniauth授權控制器編寫測試時遇到了Rspec問題。儘管存在控制器和操作,但Rspec中的ActionController :: RoutingError

繼承人我的routes.rb

MyWebApp::Application.routes.draw do 

    get "static/index" 

    match "login" => 'user_sessions#new' 

    match 'logout' => 'user_sessions#destroy' 

    match "api" => "api#content", :via => :get 
    match "api/node_tree" => "api#node_tree", :via => :get 

    match "/auth/:provider/callback" => "oauth_authorizations#create" 
    match "/auth/failure" => "oauth_authorizations#failure" 
    match "/auth/:provider" => "oauth_authorizations#blank" 

    resources :users do 
    resources :apps do 
     resources :nodes 
    end 
    end 

    resources :user_sessions 
end 

oauth_authorization_controller_spec.rb

it "should create a new authorization entry for the user" do 
    expect {get :create }.to change(Authorization, :count).by(1) 
end 

oauth_authorization_controller.rb

class OauthAuthorizationsController < ApplicationController 

    def create 

    end 

end 

當我運行我的天賦,我得到以下錯誤

Failures: 

    1) OauthAuthorizationsController when a current user session already exists should create a new authorization entry for the user 
    Failure/Error: expect {get :create }.to change(Authorization, :count).by(1) 
    ActionController::RoutingError: 
     No route matches {:controller=>"oauth_authorizations", :action=>"create"} 

任何人都可以幫助我找出背後的原因是什麼,因爲從控制器代碼清楚地看到{:controller =>「oauth_authorizations」,:action =>「create」}確實存在。

+0

張貼您的路線文件...相關路線是否存在? –

+0

我編輯了我的問題併發布了我的路線文件。 – Sunil

回答

0

的問題是,在路由指定的供應商參數,

match "/auth/:provider/callback" => "oauth_authorizations#create" 

沒有從測試通過。

傳遞它固定測試。

get :create, :provider => omniauth_hash['provider'] 

因此,測試將被重寫爲。

it "should create a new authorization entry for the user" do 
    expect {get :create, provider => omniauth_hash['provider'] }.to change(Authorization, :count).by(1) 
end 

可能這會幫助一些人。

1

嘗試使用後更換GET HTTP動詞:

expect {post :create }.to change(Authorization, :count).by(1) 
+0

嘿,試過了,但它的例外。無論如何,我並沒有將其指定爲資源。 – Sunil

相關問題