Explicitly require files and manage LOAD_PATH in tests which do not boot a framework
Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.ruby-version
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile
spec_requirer.gemspec

README.md

SpecRequirer

Gem Version Code Climate Build Status Coverage Status

Helps require files and manage the $LOAD_PATH of unit tests which do not boot a framework.

For example in Rails if you want fast unit tests you do not boot Rails. However this means that "app/models" etc. are not added to the LOAD_PATH. This typically means you need to either add the paths or use require_relative.

  • Explicit requiring of files
  • Additional meta-information about the required files
  • Adds paths to the LOAD_PATH

Require only what you need, keep it light and explicit.

Installation

gem 'spec_requirer', group: :test, github: 'HouseTrip/spec_requirer', tag: 'v0.0.2'

This is not released to Rubygems yet.

Usage

In your test_helper / spec_helper:

require 'pathname'
require 'spec_requirer'

SpecRequirer.setup(app_root: File.join(File.dirname(__FILE__), ".."),
                   components: ['models', 'services', 'presenters'])

include SpecRequirer

In a Rails app the app_root would be the app directory.

Require helpers

For each component a require helper is added which lets you do:

require_models 'user', 'reward', 'prize'

require_presenters 'user_presenter'

require_services 'create_user'

$LOAD_PATH helpers

Similarly a "utilizes" method is available for each component which adds a directory to the $LOAD_PATH. You can then require the file as usual:

utilizes_models
utilizes_presenters
utilizes_services

require 'user'
require 'user_presenter'
require 'create_user'

Or you can use the one-line version as such:

utilizes :models, :presenters, :services

Configuration

Commented out options are not yet implemented.

require 'pathname'
require 'spec_requirer'

SpecRequirer.configure do |config|
  config.app_root = Pathname(__FILE__).join('..')
  # config.add_components(in: ‘app’, except: %’w(views, assets))
  # config.add_component('config/initializers', as: 'initializer')
  # config.patch_kernel = true
end

SpecRequirer.setup
include SpecRequirer

You can also pass some configuration directly to setup as such:

SpecRequirer.setup(app_root: app_root, components: ['models', 'presenters'])

Contributing

Yes, please.