Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: testing GET request on a namespaced controller

I have a failing test that I'm struggling to understand. I have a controller class Users::QueriesController < ApplicationController at app/controllers/users/queries_controller.rb which has a show action, and a corresponding namespaced route:

namespace :users do
  resources :queries
end

I also have a test at test/controllers/users/queries_controller_test.rb:

require 'test_helper'

class Users::QueriesControllerTest < ActionController::TestCase

  test "accessing :show action" do
    get :show
    assert_response :success
  end

end

Running this test results in ActionController::UrlGenerationError: No route matches {:controller=>"users/queries", :action=>"show"}.

Running rake routes includes this line: users_query GET /users/queries/:id(.:format) users/queries#show.

What's going wrong here? I'm using Rails 4.0.0.

like image 447
6twenty Avatar asked Feb 17 '23 00:02

6twenty


1 Answers

I think you need to provide an id to the show action

  test "accessing :show action" do
    get :show, {:id => 1}
    assert_response :success
  end

That's the right way prior to rails 4.

Please have a try, and let me know the outcome.

like image 122
Henry Avatar answered Feb 24 '23 03:02

Henry