In my Ruby on Rails project I have some files in spec/support
that are dependent on other files within the same directory.
#spec/support/page_objects/foo.rb
class Foo
include Bar
end
#spec/support/page_objects/bar/bar.rb
module Bar
def hello_world
"Hello World"
end
end
In order for Foo.new.hello_world
to work in a spec, I must explicitly prefix the dependent file with require_relative("bar/bar")
.
#spec/support/page_objects/foo.rb
require_relative("bar/bar")
class Foo
include Bar
end
I would prefer not to have to explicitly call require
in all of my dependent files.
All of my spec/support
files are loaded per this line of the spec/rails_helper.rb
.
#spec/rails_helper.rb
Dir[Rails.root.join("spec", "support", "**", "*.rb")].sort.each { |f| require f }
I have determined that changing this line and explicitly loading dependencies first, will work.. But I am looking for a cleaner more long term solution.
I do not want to have to explicitly call require
in all of my dependent files, nor do I want to have to name all of my dependencies in the above block for the sake of load order.
I just want it to work typical to how app/models
works when using dependencies in development.
Is there a way to accomplish this?
question from:
https://stackoverflow.com/questions/65837213/how-do-i-load-order-dependent-support-files-in-rspec-without-explicitly-requirin 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…