DEV Community

Dillon Adams
Dillon Adams

Posted on • Originally published at codejanitor.dev on

Unable to find Ruby class that definitely exists

Recently I have been learning Ruby on Rails! With that learning comes a lot of lessons, and this one was both fun and frustrating for me. While working in the Ruby on Rails project, I created a new class and RSpec tests for that class.

/app/thing/thingy_doer.rb

class Thing::ThingDoer
# ...
end

Enter fullscreen mode Exit fullscreen mode

/spec/thing/thing_doer_spec.rb

RSpec.describe Thing::ThingDoer do
# ...
end

Enter fullscreen mode Exit fullscreen mode

I ran the tests and got the following error:

NameError:
  uninitialized constant Thing::ThingDoer

Enter fullscreen mode Exit fullscreen mode

Crazy right? I specified the class name in the test exactly as it is spelled in the class, but the RSpec tests can’t find the class. Why?!

The issue is that Rails has a loading convention that matches the file name to the class name. Due to the mismatch between the name of the thingy_doer.rb file and the ThingDoer class, Rails can’t find the class.

The solution is is correct the typo in the file name. Once the file is renamed from thingy_doer.rb to thing_doer.rb, everything now works as expected!

5 examples, 0 failures

Enter fullscreen mode Exit fullscreen mode

Top comments (0)