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.


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'
链接地址: http://www.djcxy.com/p/97086.html

上一篇: 如何在awk中为每个循环执行一个命令?

下一篇: 在Ruby中从文件名动态创建自动加载命令