Skip to content

Instantly share code, notes, and snippets.

@jsborjesson
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save jsborjesson/10381219 to your computer and use it in GitHub Desktop.

Select an option

Save jsborjesson/10381219 to your computer and use it in GitHub Desktop.
Minitest Spec for Rails.
require 'simplecov'
SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'rails/test_help'
require 'factories'
# Doc-style test reporting (similar to --format=documentation in rspec)
require 'turn'
# Available styles: :outline, :progress, :dotted, :pretty, :marshal, :cue
Turn.config.format = :outline
Turn.config.natural = true
# Reset the database for each test
require 'database_cleaner'
DatabaseCleaner.strategy = :transaction
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
end
class Minitest::Spec
include ActiveSupport::Testing::Assertions # assert_difference etc...
include ActiveSupport::Testing::SetupAndTeardown # before, after
# Support rspec-style context-blocks
class << self
alias_method :context, :describe
end
before { DatabaseCleaner.start }
after { DatabaseCleaner.clean }
end
# A bit more dry validation tests
# m = Message.create(text: '')
# m.must_have_invalid :text
module Minitest::Assertions
def assert_has_invalid(field, model)
model.must_be :invalid?
model.errors[field].must_be :present?
end
end
ActiveRecord::Base.infect_an_assertion :assert_has_invalid, :must_have_invalid
# RSpec like `expect{}.to change` using this syntax:
# -> { Message.create(text: 'A message') }.must_change 'Message.count', +1
class Proc
infect_an_assertion :assert_difference, :must_change
infect_an_assertion :assert_no_difference, :wont_change
end
# Make the controller methods (get, post etc...) work
class ControllerSpec < Minitest::Spec
include Rails.application.routes.url_helpers
include ActionController::TestCase::Behavior
before do
@routes = Rails.application.routes
end
end
Minitest::Spec.register_spec_type(/Controller$/, ControllerSpec)
### Gemfile
group :test do
gem 'minitest'
gem 'minifacture'
gem 'database_cleaner'
gem 'capybara_minitest_spec'
gem 'simplecov', require: false
gem 'turn'
end
@jsborjesson
Copy link
Author

Manual configuration of Minitest for Rails, it uses the plain minitest gem so no need for minitest-rails or minitest-spec-rails - what you see is what you get.

It covers most of the things I missed from RSpec and is set up for both readable output and readable tests, but is still very minimal and quick.

I stole much of it from this gist: https://gist.github.com/sethbro/1784986

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