Skip to content

Instantly share code, notes, and snippets.

@danielevans
Last active December 23, 2015 20:49
Show Gist options
  • Save danielevans/6692625 to your computer and use it in GitHub Desktop.
Save danielevans/6692625 to your computer and use it in GitHub Desktop.
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/spec"
# require "mocha/setup"
# require "turn/autorun"
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
# fixtures :all
# Add more helper methods to be used by all tests here...
extend MiniTest::Spec::DSL
# include FactoryGirl::Syntax::Methods
class << self
remove_method :describe
end
register_spec_type self do |desc|
desc < ActiveRecord::Base if desc.is_a? Class
end
end
require 'test_helper'
# class UsersControllerTest < ActionController::TestCase
# test "raises an error if no user with that unique token is found" do
# lambda {
# get(:unsubscribe, :unique_token => "bob")
# }.must_raise ActiveRecord::RecordNotFound
# end
# end
describe UsersController do
it "raises an error if no user with that unique token is found" do
lambda {
get(:unsubscribe, :unique_token => "bob")
}.must_raise ActiveRecord::RecordNotFound
end
end
@blowmage
Copy link

The reason the controller tests don't work when using the spec DSL is that you haven't told Minitest to use ActionController::TestCase when describing a controller. You need to call register_spec_type and get that registered.

class ActionController::TestCase
  # Use AC::TestCase for the base class when describing a controller
  register_spec_type(self) do |desc|
    Class === desc && desc < ActionController::Metal
  end
  register_spec_type(/Controller( ?Test)?\z/i, self)
end

ActionController::TestCase does a bunch of name matching to figure out which controller is being tested and creating an instance of it for the tests. But AC::TC never intended to do this for test classes that were created by the spec DSL. You may still have problems when using describe because of how the class that describe creates is named. The best way around that is to override how AC::TC derives the class.

class ActionController::TestCase
  # Resolve the controller from the test name when using the spec DSL
  def self.determine_default_controller_class(name)
    controller = determine_constant_from_test_name(name) do |constant|
      Class === constant && constant < ActionController::Metal
    end
    raise NameError.new("Unable to resolve controller for #{name}") if controller.nil?
    controller
  end
end

There is more to do if you want to use the spec DSL for helper/view tests, or integration tests. You can either look at the source of minitest-rails and copy that code to your project, or you can make your life much easier and simply include it in your helper.

ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)

require 'rails/test_help'
require "minitest/rails"
# require "mocha/setup"
# require "turn/autorun"

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  # fixtures :all

  # Add more helper methods to be used by all tests here...
end

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