ActionController::UrlGenerationError in rspec - ruby-on-rails

While testing my controller action I am getting this routing error. I am not able to figure it out where I am doing it wrong
route
get ':user_type', to: 'user_posts#index', param: :user_type, constraints: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) }, as: :custom_posts
Spec:
describe "GET #index: enabled" do
it "returns http success" do
get :index, params: {user_type: 'abc'}
expect(response).to have_http_status(:success)
end
end
Error:
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"user_posts"}
Also, I tried one solution on github by changing my route but it didn't work for me
get ':user_type', to: 'user_posts#index', param: :user_type, constraints: {user_type: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) } }, as: :custom_posts

Finally, I got an answer. Here is the detail discussion
describe "GET #index: enabled" do
it "returns http success" do
get :index, params: { user_type: 'abc', param: :user_type }
expect(response).to have_http_status(:success)
end
end

Related

Rails Api test UrlGenerationError

I am building an API and, upon writing the tests, I run into a strange UrlGenerator Error.
I have an API on version one and this is my Users controller.
class Api::V1::UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
end
Here is the spec for that users controller
require 'rails_helper'
RSpec.describe Api::V1::UsersController, type: :controller do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
#user = FactoryBot.create :user
get :show, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql #user.email
end
it { should respond_with 200 }
end
end
When I run this spec I get the following error message: `Failure/Error: get :show, format: :json
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"api/v1/users", :format=>:json}`
I have only one route for my API:
api_user GET /users/:id(.:format) api/v1/users#show {:subdomain=>"api", :format=>:json}
Does anybody know why I would be getting this error? It seems to me that, based on the route returned from the api routes list, this should be working. My routes.rb file is listed below:
namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :users, :only => [:show]
end
end
The problem is that the show route that you have defined requires an :id parameter, but the call for get :show from the test does not send it.
From Rspec, you can send the id with something like:
get :show, params: { id: #user.id }, format: :json

Rails Spec Controller Test failing with custom route

Rails 5.1
RSpec 3.6
I have a Controller:
class SessionController < ApplicationController
def new
end
end
A custom route:
get 'login' => 'sessions#new'
RSpec Test:
require 'rails_helper'
RSpec.describe SessionController, type: :controller do
describe "GET #new" do
before do
routes.draw { get "login" => "sessions#new" }
end
it "returns http success" do
get :login
expect(response).to have_http_status(:success)
end
end
end
and get error:
ActionController::UrlGenerationError: No route matches {:action=>"login", :controller=>"session"}
So "get" within a controller test seems always map to the action not the route. What should i do to get this test run? thanks in advance.
ActionController::UrlGenerationError: No route matches
{:action=>"login", :controller=>"session"}
Your controller name is SessionController, so your route should be
get 'login' => 'session#new' not get 'login' => 'sessions#new'
require 'rails_helper'
RSpec.describe SessionController, type: :controller do
describe "GET #new" do
before do
routes.draw { get "login" => "session#new" }
end
it "returns http success" do
get :login
expect(response).to have_http_status(:success)
end
end
end
Change it in your routes.rb as well.
When you are writing tests and you use the methods get, post, delete, etc., those methods assume that any parameter you pass them is the name of an action within the controller being tested. So, this works:
get :new
because it generates url_for(:controller => :sessions, :action => :new).
This doesn't work:
get '/login'
because it generates url_for(:controller => :sessions, :action => '/login').

ActionController::UrlGenerationError: No route matches action and controller

I couldn't find a solution in the other relative questions, so I'm asking my own.
The problem is pretty straightforward. This is the error I'm getting:
Failure/Error: get 'api/v2/special_keys#show'
ActionController::UrlGenerationError:
No route matches {:action=>"api/v2/special_keys#show", :controller=>"api/v2/special_keys"}
This is my routes.rb:
resources :special_keys, only: [] do
collection do
get '', to: 'special_keys#show'
end
end
This is the output from rake routes:
GET /api/v2/special_keys(.:format) api/v2/special_keys#show {:format=>"json"}
And my spec:
require 'rails_helper'
describe Api::V2::SpecialKeysController do
describe 'GET #show' do
it 'gets the policy and signature' do
get '/api/v2/special_keys'
expect(response.status).to eql 200
end
end
end
Try to rewrite your test as:
require 'rails_helper'
describe Api::V2::SpecialKeysController do
describe 'GET #show' do
it 'gets the policy and signature' do
get '/api/v2/special_keys', {format: :json}
expect(response.status).to eql 200
end
end
end
Try:
resource :special_keys, only: [:show]
The singular tells the app, that there is only one. So it will only generate a show action that needs no id and no indexaction at all.

ActionController::UrlGenerationError: error No routes matches

I get the below error ActionController::UrlGenerationError.
ActionController::UrlGenerationError:
No route matches {:action=>"/accounts/100", :controller=>"accounts"}
Below is my code which throws this error.
it "can find an account" do
Account.should_receive(:find, with: {id: 2055, authorization: #auth}
get "/accounts/100", nil, {"AUTH_TOKEN" => #auth}
hashed_response = {
"#type" => "test",
"createdAt" => "2014-07-24T15:26:49",
"description" => "Something about test",
"disabled" => false
}
expect(response.status).to eq 200
expect(response.body).to eq(hashed_response.to_json);
end
I did a google on this and came to know that there is no routes defined for this. Below is my config/routes.rb file
Rails.application.routes.draw do
resources :accounts do
resources :holders
end
end
I presume this error is coming from a controller spec? If so, you simply use a symbol representing the action you want to call, not the URL itself.
eg. this is a show action, so you would use:
get :show, id: 100

Devise, Rails-API, Routing issue

I have the following routes setup inside a rails api app:
constraints subdomain: 'api', path: '/' do
namespace 'api', path: '/' do
scope module: 'v1' , constraints: ApiConstraints.new(version: 1, default: true) do
devise_scope :user do
post 'sign_in' => 'sessions#create'
delete 'sign_out' => 'sessions#destroy'
end
resources :question
end
end
end
# rake routes
Prefix Verb URI Pattern Controller#Action
api_sign_in POST /sign_in(.:format) api/v1/sessions#create {:subdomain=>"api"}
api_sign_out DELETE /sign_out(.:format) api/v1/sessions#destroy {:subdomain=>"api"}
When I got to execute this test:
require 'rails_helper'
RSpec.describe Api::V1::SessionsController, :type => :controller do
describe '#create' do
it 'creates a session' do
#request.env["devise.mapping"] = Devise.mappings[:user]
user = User.create(email: 'rob#edukate.com', password: 'password')
post :create, action: :create, user_login: { password: user.password, email: user.email}
expect(response).to be_success
end
end
end
I get this error:
F
Failures:
1) Api::V1::SessionsController#create creates a session
Failure/Error: post :create, action: :create, user_login: { password: user.password, email: user.email}
AbstractController::ActionNotFound:
Could not find devise mapping for path "/sign_in?user_login%5Bemail%5D=rob%40edukate.com&user_login%5Bpassword%5D=password".
I have tried many variations of routes but I cannot get this simple test to pass. If I cannot get it completed like this I'll just roll my own Auth; but it appears devise is doing something odd with a Abstract controller. Thanks in advance!

Resources