abhishek
abhishek

Reputation: 301

RSpec not able to find class inside modules

I have a class in app/services/a/b/c.rb

Rspec file is in spec/services/a/b/c_spec.rb

My class looks like this:

module a
 module b
   class c
   end 
 end
end

My rspec looks like this:

require 'rails_helper'

describe a::b::c do
  describe '#test' do
    it 'should test func' do
    end
  end
end

But everytime I run the test I get this error::

uninitialized constant a::b::c

Can anyone help me with this?

Upvotes: 2

Views: 3242

Answers (3)

Paul Danelli
Paul Danelli

Reputation: 1133

Incase anyone else has this issue; I ended up fixing it by restarting my laptop.

For some reason, it wasn't finding classes nested inside of modules, but the restart fixed it. Wasted 2 hours on that.

Upvotes: 0

Stefan Staub
Stefan Staub

Reputation: 687

I faced the exact same issue, and the fix was pretty simple. I forgot to put:

require "rails_helper"

in the test file. After adding the require statement, it correctly loaded the class. (I'm aware that the questioner correctly added the require statement). May this answer help others.

Upvotes: 2

Sarvnashak
Sarvnashak

Reputation: 829

@Abhishek I feel the problem is that your class isn't required/loaded by Rails. Try requiring this file in spec_helper.rb.

Alternatively you can add that folder to eager_load_paths and set config.eager_load = true in config/environment/test.rb

You can confirm my hunch by using rails console in test environment and then check that invoking A::B::C gives uninitialized constant A::B::C.

Now try by requiring the app/services/A/B/C.rb file and this time it won't give the error.

Upvotes: 1

Related Questions