简体   繁体   中英

uninitialized constant error when running tests on Ruby on rails 5

I'm fairly new to Ruby on rails and running into uninitialized constant issue when running tests. This is ROR is created with --api option and this what I've done so far:

After creating models for locations, users, and visits(this is many-to-many table for locations and users),

rails g controller v1/locations
rails g controller v1/visits
rails g controller v1/users

I ran three command lines to generate corresponding controllers. As you can see, I'm using v1 as a namespace assuming there will be v2, v3 and so on.

Then, I ran the following command line expecting no errors.

rails test test/controllers

and voila. I'm getting the following error message :(

Running via Spring preloader in process 66797
/Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `block in load_missing_constant': uninitialized constant V1 (NameError)
    from /Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
    from /Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `rescue in load_missing_constant'
    from /Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `load_missing_constant'
    from /Users/sangminkim/Desktop/friend-finder/test/controllers/v1/locations_controller_test.rb:3:in `<main>'
    from /Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    from /Library/Ruby/Gems/2.3.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'

So... uninitialized constant V1 (NameError)?

Finding it odd, I checked out the auto-generated test file.

require 'test_helper'

class V1::LocationsControllerTest < ActionDispatch::IntegrationTest
  # test "the truth" do
    # assert true
  # end
end

It looks like ROR doesn't understand V1::LocationsControllerTest part...

I tried changing the folder name(v1) to V1 thinking it might be an uppercase issue but no luck.

What am I missing here?

Thanks,

Update::

class V1::LocationsController < ApplicationController
end

@Raccoon if you forgot to add a route to your routes.rb then it won't work because when you write any test case like below,

test "should get index" do
    get users_url
    assert_response :success
end

You send an HTTP request to the application, which will find an appropriate route for it and dispatch that request to specified controller and action for that route.

Can you try changing your controller to:

require 'test_helper'

module V1
  class LocationsControllerTest < ActionDispatch::IntegrationTest
    # test "the truth" do
      # assert true
    # end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM