2013-06-25

my configuration for minitest in a rails project

Here I want to document my minitest configuration because I think it is elegant and if this helps anyone, then I am very happy.

spec/spec_helper.rb


unless @no_env
  ENV['RAILS_ENV'] = 'test'
  require File.expand_path('../../config/environment', __FILE__)
end

require 'minitest/autorun'
require 'minitest/spec'
require "minitest/pride"

# if this file was executed directly
if __FILE__ == $0
  $LOAD_PATH.unshift('spec')
  Dir.glob('./spec/**/*_spec.rb') { |f| require f }
end


test group in my Gemfile


group :test do
  gem 'minitest'
end


spec/models/foo.rb


standard rails model spec.

require_relative '../spec_helper'

describe Foo do
  # specs here
end


spec/lib/foo_module.rb


example spec for a lib that doesn't require the rails env

require_relative '../spec_helper'
require_relative '../../lib/foo_module.rb'
@no_env = true

describe FooModule do
  # spec for module that is decoupled from the rails env
end
The best thing here is having no reliance on rake. The spec files and the helper will be called directly with ruby.
Examples:
"ruby spec/spec_helper.rb" - will run all specs
"ruby spec/lib/foo_spec.rb" - will just run that spec

And vim makes this easy when editing a spec file I just run "!ruby %" to run that spec file
A final note: I realize "ruby spec/spec_helper.rb" is a bit much to type, so of course I simply made a zsh alias called "specs" for it.