Last active
August 29, 2015 13:58
-
-
Save jsborjesson/10381219 to your computer and use it in GitHub Desktop.
Minitest Spec for Rails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manual configuration of Minitest for Rails, it uses the plain
minitestgem so no need forminitest-railsorminitest-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