Getting Uninitialized Constant Error When Trying to Run Tests

Getting uninitialized constant error when trying to run tests

This can happen if modules are declared in a single statement when the parent module they are nested inside has not yet been loaded. I haven't looked at the code in those gems, but my hunch is that's what is happening. Chuck's solution would suggest that. calling gem 'test-unit' first will load the parent module, so the setup of zen test ends up working ok.

e.g.

module Foo::Bar
def do_stuff
# insert awesomeness here...
end
end

Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)

A safer way to declare this is

module Foo
module Bar
def do_stuff
# insert awesomeness here...
end
end
end

May be a bug in Zentest that needs patching.

uninitialized constant error when running tests on Ruby on rails 5

@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.

NameError: uninitialized constant when trying to execute rails tests

Finally I found the reason why all my tests failed with an error. I was referencing to a constant declared in the Availability concern using Availabiliy:: , instead of using Business::. Also, my tests were not in sync with the current code, which made finding the bug a lot more difficult than it should. Replacing all the references to Availability with Business solved the problem. The lesson here is: keep always application and testing code in sync.

Thank you to Lichtamberg for all his kind support.

RSpec error: Uninitialized constant ModuleName::Chef (NameError)

I solved this issue.

It turns out that I needed to modify my spec_helper to be as follows:

require 'rspec'
require 'chef/knife'
require 'chef/knife/class_name'

This confused me because I don't have any other files in the 'chef/knife' folder, but as it turns out that is just how ruby accesses the knife part of the chef gem. If anyone else has something to add that would further enlighten me, please do. I am still sort of confused about it

Rails Test Error, NameError: uninitialized constant User::FILL_IN

You are not supposed to actually use the words FILL_IN. You are supposed to change FILL_IN the whatever you need to make the tests pass. You probably need something like:

update_columns(activated: true, activated_at: Time.now)

Test ActionController::RoutingError: uninitialized constant V1::LocationsController

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



Related Topics



Leave a reply



Submit