0

Continued from uninitialized constant error when running tests on Ruby on rails 5 ...

I'm writing test cases for my api controller with namespace - v1 but it seems ROR doesn't understand the namespace V1 exists.

When I run "rails routes", this is what I get:

             v1_locations GET    /v1/locations(.:format)                                                                  v1/locations#index
                          POST   /v1/locations(.:format)                                                                  v1/locations#create
              v1_location GET    /v1/locations/:id(.:format)                                                              v1/locations#show
                          PATCH  /v1/locations/:id(.:format)                                                              v1/locations#update
                          PUT    /v1/locations/:id(.:format)                                                              v1/locations#update
                          DELETE /v1/locations/:id(.:format)                                                              v1/locations#destroy

and this is what I have in my test file...

class V1::LocationControllerTest < ActionDispatch::IntegrationTest

  def setup()
    # A bunch of stuff here
  end

  test "cannot retrieve a list of locations without a valid token" do
    get v1_locations_url
    assert_response :unauthorized
  end

  # More tests here...

end

and this is what my controller looks like

class V1::LocationController < ApplicationController

    # To be filled once tests run and fail

end

and this is the error message I'm getting...

Error:
V1::LocationControllerTest#test_cannot_create_a_location_without_a_valid_token:
ActionController::RoutingError: uninitialized constant V1::LocationsController
    test/controllers/v1/location_controller_test.rb:32:in `block in <class:LocationControllerTest>'


bin/rails test test/controllers/v1/location_controller_test.rb:31

It's also funny that I get page not found error when I send a GET request to /v1/locations

Mmm... What am I missing here?

Thanks,

Raccoon
  • 1,367
  • 4
  • 22
  • 45

1 Answers1

3

Check your error log it says ActionController::RoutingError: uninitialized constant V1::LocationsController, while you controller name is LocationController.

Please rename it to LocationsController and rename a file to locations_controller.rb.

You can get more insights about that here

Krupa Suthar
  • 670
  • 3
  • 14