Skip to content

Instantly share code, notes, and snippets.

@steventux
Created June 25, 2012 19:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save steventux/2990759 to your computer and use it in GitHub Desktop.
Save steventux/2990759 to your computer and use it in GitHub Desktop.
MiniTest + Rails + Devise test_helper
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require File.dirname(__FILE__) + '/blueprints'
require 'faker'
require 'rails/test_help'
require 'minitest/autorun'
require 'minitest/pride'
class MiniTest::Unit::TestCase
include MiniTest::ActiveRecordAssertions
DatabaseCleaner.strategy = :transaction
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
class MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
alias :method_name :__name__ if defined? :__name__
def build_message(*args)
args[1].gsub(/\?/, '%s') % args[2..-1]
end
end
class ControllerSpec < MiniTest::Spec
include ActionController::TestCase::Behavior
include Devise::TestHelpers
include Rails.application.routes.url_helpers
# 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 )
@route
Copy link

route commented Oct 3, 2012

Hi Steve, there's one problem here for controllers, you have to use

class ControllerSpec < MiniTest::Spec
  before do
    @routes = Rails.application.routes
  end
end

instead of include Rails.application.routes.url_helpers.
The main problem is if helper method starts from test.*_path it become a method that minitest will invoke and consider like a test.

@orbanbotond
Copy link

The inclusion of Devise:TestHelpers gives us this exception:
/gems/minitest-4.6.0/lib/minitest/spec.rb:158:in before': wrong number of arguments (2 for 1) (ArgumentError) from /Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/devise-2.2.3/lib/devise/test_helpers.rb:12:inblock in included'
from /Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/devise-2.2.3/lib/devise/test_helpers.rb:11:in class_eval' from /Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/devise-2.2.3/lib/devise/test_helpers.rb:11:inincluded'
from /Users/boti/Rails/clients/kevin/search_server/test/spec_helper.rb:35:in include' from /Users/boti/Rails/clients/kevin/search_server/test/spec_helper.rb:35:inclass:ControllerSpec'
from /Users/boti/Rails/clients/kevin/search_server/test/spec_helper.rb:33:in <top (required)>' from /Users/boti/Rails/clients/kevin/search_server/test/functional/suppliers_controller_spec.rb:1:inrequire'
from /Users/boti/Rails/clients/kevin/search_server/test/functional/suppliers_controller_spec.rb:1:in <top (required)>' from -e:inrequire'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment