Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches {:action=>"show" in RSpec, fine in browser

I have a users_controller.rb that includes:

before_action :set_user, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource

def index
  @users = User.all
end

def show
end

def new
  @user = User.new
end

private
def set_user
  @user = User.find(params[:id])
end

My config/routes includes:

devise_for :users

devise_scope :user do
  authenticated :user do
    root 'switchboard#show', as: :authenticated_root
  end

  unauthenticated do
    root 'devise/sessions#new', as: :unauthenticated_root
  end
end

resources :users

When I navigate to http://localhost:3000/users I get a list of the users as I expect. But when I run the RSpec test:

require 'rails_helper'

RSpec.describe UsersController, :type => :controller do

  describe "GET show" do
    it "returns http success" do
      get :show
      expect(response).to be_success
    end
  end
end

I get the error No route matches {:action=>"show", :controller=>"users"} on the RSpec line get :show.

I've found countless queries about No route matches {:action=>"show", each one with a different cause, but none of them seems to match mine. What do I do to fix this (other than not test the feature!)

like image 274
digitig Avatar asked Dec 06 '25 03:12

digitig


1 Answers

Because you have no route that matches controller user action show

However, you do have a route that matches user/:id (it's the :id you're missing)

So you could do...

get :show, id: 1
like image 182
SteveTurczyn Avatar answered Dec 09 '25 19:12

SteveTurczyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!