即使控制器和操作存在,Rspec中的ActionController :: RoutingError也是如此

时间:2012-12-30 16:11:05

标签: ruby ruby-on-rails-3 rspec rspec-rails

我在为Omniauth授权控制器编写测试时遇到了Rspec的问题。

继承我的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 =&gt;“oauth_authorizations”,:action =&gt;“create”}确实存在。

2 个答案:

答案 0 :(得分:1)

尝试用帖子替换get http动词:

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

答案 1 :(得分:0)

问题在于路由中指定的provider参数

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

这可能对某人有所帮助。