Seal
Seal

Reputation: 1060

Rspec controller action not found

test is failing becasue it says the action does not exist, when it clearly does. Is it becasue it is a nested route? Any thoughts?

Update:

I moved resources :orders outside of the nested route and tests passed. So it has something to do with it being nested.

OrderController

def index
    if current_printer
      @orders = Order.all
      @printer = Printer.find(params[:printer_id])
    end
    if current_user
      @orders = Order.where(user_id: params[:user_id])
    end
end

OrdersController Spec

require 'rails_helper'

RSpec.describe OrdersController, :type => :controller do
  describe "unauthorized user" do
    before :each do
      # This simulates an anonymous user
      login_with_user nil
      binding.pry
    end

    it "should be redirected back to new user session" do
      get :index
      expect( response ).to redirect_to( new_user_session_path )
    end
  end
end

Routes

resources :users, only: [:index, :show] do
      resources :orders
end

Error

Failures:

  1) OrdersController unauthorized user should be redirected back to new user session
     Failure/Error: get :index

     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"orders"}

Upvotes: 1

Views: 1440

Answers (1)

Seal
Seal

Reputation: 1060

When testing controllers that have nested routes you must pass in a hash of the url params.

for example my routes looked like this

user_orders GET /users/:user_id/orders(.:format) orders#index

so in my test I passed in a hash with user_id

get :index, { user_id: 1 }

Tests passing :)

Upvotes: 2

Related Questions