Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create autoload command from filenames in Ruby

I am testing a web application with ruby, rspec, capybara and selenium and i ran into an uninitialized constant ActiveAdminLoginPage Exception that i don't know how to solve.

In spec_helper.rb i am requiring the following:

Dir[File.join(Dir.pwd, 'spec/page_objects/**/*.rb')].each { |f| require f }

I have 2 classes

spec/page_objects/products/active_admin_login_page.rb  

module Products
  class ActiveAdminLoginPage < ::ActiveAdminLoginPage
  ...
  end
end 

inherits from

spec/page_objects/active_admin_login_page.rb

unfortunately the sub class is loaded before the parent class.

How do i create a autoload command dynamically from all filenames in a directory? I would like to replace this command:

Dir[File.join(Dir.pwd, 'spec/page_objects/**/*.rb')].each { |f| require f }

with a autoload command.

like image 203
Andree Wille Avatar asked Oct 17 '22 07:10

Andree Wille


1 Answers

How about you use require to load your dependency in the file that needs it?

require loads a file only once, so you shouldn't encounter any side effects.

Or, even better, you can use auto_load, which uses require under the hood, but it does it in a smarter way

autoload :ActiveAdminLoginPage, 'active_admin_login_page'
like image 66
Mihai Dinculescu Avatar answered Oct 21 '22 08:10

Mihai Dinculescu