Sunil
Sunil

Reputation: 3434

ActionController::RoutingError in Rspec eventhough the controller and action exists

I have run into a problem with Rspec while writing tests for my Omniauth authorizations controller.

Heres my 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

When i am running my spec, I get the following error

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"}

Could any one please help me find out whats the reason behind this, because as its clear from the controller code, {:controller=>"oauth_authorizations", :action=>"create"} does exist.

Upvotes: 2

Views: 544

Answers (2)

Sunil
Sunil

Reputation: 3434

The problem was that the provider parameter specified in the route,

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

was not passed from the test.

Passing it fixed the test.

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

So the test would be re written as.

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

May be this would help some one.

Upvotes: 0

Erez Rabih
Erez Rabih

Reputation: 15788

Try to replace the get http verb with a post:

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

Upvotes: 1

Related Questions