zOs0
zOs0

Reputation: 324

How to require modules in app/lib folder to rspec in Rails 5

I moved my modules to

app/lib/parsers

folder.

Model classes responds to module methods, but when i try

rspec
I have issue

Failure/Error: include MainModule::Submodule

NameError:
uninitialized constant ModelName::MainModule

module in lib/parsers looks like this

Module Parsers
  Module Parser1
    def foo
    end
end
  Module Parser2

   def bar
   end
 end
end

first model includes first parser by that way

Class Model1 < ApplicationRecord
  include Parsers::Parser1
end

and the second one by the same way What is the best way to require that modules in Rspec?

Upvotes: 1

Views: 1515

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

What is the best way to require that modules

Use rails' auto-loading mechanism. Put your Parsers::Parser1 to app/lib/parsers/parser1.rb and Parsers::Parser2 to app/lib/parsers/parser2.rb. See how module's full name mirrors where it's stored? That's how rails is able to find it.

Or you can require the file explicitly

# my_spec.rb
require_dependency Rails.root.join('app', 'lib', 'parsers')

RSpec.describe ... 

Upvotes: 2

Related Questions