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

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay