ootoovak
5/21/2013 - 12:02 AM

Helper methods to load as few files as possible to get tests to run. Some create require paths to load files and others to init required con

Helper methods to load as few files as possible to get tests to run. Some create require paths to load files and others to init required constants.

APP_ROOT       = '../..'
APP_MODEL_PATH = 'app/models'
LIB_MODEL_PATH = 'lib/models'

def defaults
  { app_path: APP_MODEL_PATH }
end

def require_model(name, opts = {})
  opts[:app_path] = LIB_MODEL_PATH if opts.delete(:lib)
  require_resource(name, opts)
end

def require_resource(name, opts = {})
  opts = defaults.merge(opts)
  require construct_path(name, opts)
end

def construct_path(name, opts = {})
  app_path_to_file = [ opts[:app_path] ]
  app_path_to_file << opts[:sub_dir] if opts[:sub_dir]
  app_path_to_file << name
  app_path_to_file = app_path_to_file.join('/') + '.rb'
  absolute_path(app_path_to_file)
end

def absolute_path(app_path_to_file)
  File.expand_path("#{APP_ROOT}/#{app_path_to_file}", __FILE__)
end

def required_constants(class_names)
  class_names.each do |class_name|
    unless Object.const_defined?(class_name)
      Object.const_set(class_name, Class.new)
    end
  end
end
require 'fast_spec_helper'
require 'date'
require_model 'generate_required_daily_lists'
required_constants %w(Distributor PackingList DeliveryList)

---- OR ----

require 'fast_spec_helper'
require_model 'exporter', sub_dir: 'sales_csv'
require_model 'delivery_exporter', sub_dir: 'sales_csv'
required_constants %w(DeliverySort DeliveryGenerator)