Skip to content

Instantly share code, notes, and snippets.

@gnepud
Forked from sethbro/test_helper.rb
Created April 6, 2012 08:57
Show Gist options
  • Save gnepud/2318284 to your computer and use it in GitHub Desktop.
Save gnepud/2318284 to your computer and use it in GitHub Desktop.
MiniTest::Spec with Rails 3.2 setup
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/autorun'
require 'minitest/pride'
require 'capybara/rails'
class MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
include ActiveSupport::Testing::Assertions
include ActiveRecord::TestFixtures
alias :method_name :__name__ if defined? :__name__
self.fixture_path = File.join( Rails.root, 'test', 'fixtures' )
class << self
alias :context :describe
end
# cf: https://github.com/blowmage/minitest-rails/issues/12
def build_message(*args)
args[1].gsub(/\?/, '%s') % args[2..-1]
end
end
class ControllerSpec < MiniTest::Spec
include Rails.application.routes.url_helpers
include ActionController::TestCase::Behavior
# Rails 3.2 determines the controller class by matching class names that end in Test
# This overides the #determine_default_controller_class method to allow you use Controller
# class names in your describe argument
# cf: https://github.com/rawongithub/minitest-rails/blob/gemspec/lib/minitest/rails/controller.rb
def self.determine_default_controller_class(name)
if name.match(/.*(?:^|::)(\w+Controller)/)
$1.safe_constantize
else
super(name)
end
end
before do
@controller = self.class.name.match(/((.*)Controller)/)[1].constantize.new
@routes = Rails.application.routes
end
subject do
@controller
end
end
# Functional tests = describe ***Controller
MiniTest::Spec.register_spec_type( /Controller$/, ControllerSpec )
class AcceptanceSpec < MiniTest::Spec
include Rails.application.routes.url_helpers
include Capybara::DSL
before do
@routes = Rails.application.routes
end
end
# Integration/Acceptance tests = describe '*** Integration'
MiniTest::Spec.register_spec_type( /Integration$/, AcceptanceSpec )
class HelperTest < MiniTest::Spec
include ActionView::TestCase::Behavior
end
# Helper tests = describe '*** Helper'
MiniTest::Spec.register_spec_type( /Helper$/, HelperTest )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment