# Make Rails Use MiniTest::Spec! Rails 4 was on its way to using MiniTest::Spec as the superclass for ActiveSupport::TestCase. But in one of many reversals, the work was [pulled by this commit](http://github.com/rails/rails/commit/b22c527e65a41da59dbfcb078968069c6fae5086). DHH says that [Rails is omakase](http://david.heinemeierhansson.com/2012/rails-is-omakase.html) and I am cool with that. But since Ruby is wide open for us to change it and we are free in DHH's words to "freedom patch" what we see fit, we can fix Rails. The minitest-spec-rails gem makes it easy to use the MiniTest::Spec DSL within your existing Rails 3 or 4 test suite. It does this by forcing ActiveSupport::TestCase to subclass MiniTest::Spec. [![Build Status](https://secure.travis-ci.org/metaskills/minitest-spec-rails.png)](http://travis-ci.org/metaskills/minitest-spec-rails) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/metaskills/minitest-spec-rails) ## Usage Existing or new Rails 3 or 4 applications that use the default Rails testing structure can simply drop in the minitest-spec-gem and start writing their tests in the new spec DSL. Since MinitTest::Spec is built on top of MiniTest::Unit, a replacement for Test::Unit, all of your existing tests will continue to work. ### How is this different than MiniTest::Rails? To start off both Mike Moore (@blowmage) and I have worked together and we both LOVE MiniTest::Spec. Both projects aim to advocate MiniTest and make Rails integration as easy as possible. However, there are a few key differences in our projects. Some of these differences may go away in time too. As always, choose the tool you think fits your needs. So how, is minitest-spec-rails different than [minitest-rails](https://github.com/blowmage/minitest-rails)? * We aim to leverage existing Rails test directories and files! * No special test helper and/or generators. * Easy migration path for existing Rails applications. * How we go about freedom patching Rails. So the goal of this project is to make Rails 3 or 4 applications just work as if rails-core had decided to support MiniTest::Spec all along. We believe that eventually that day will come and when it does, all your tests will still work! So bundle up and get started! ```ruby gem 'minitest-spec-rails' ``` ## Test Styles This cheat sheet shows both the MiniTest::Unit assertions along with the MiniTest::Spec assertion syntax. Remember, MiniTest::Spec is build on top of MiniTest::Unit which is a Test::Unit replacement. That means you can mix and match styles as you upgrade from Test::Unit to a more modern style. For example, both of these would work in MiniTest::Spec and are interchangeable. ```ruby # MiniTest::Unit Assertion Style: assert_equal 100, foo # MiniTest::Spec Assertion Style: foo.must_equal 100 ``` All existing Rails test cases that subclass ActiveSupport::TestCase will continue to work and I personally suggest that you still **use the subclass convention** vs the outer describe test case convention. However either will work. ```ruby require 'test_helper' class UserTest < ActiveSupport::TestCase let(:user_ken) { User.create! :email => 'ken@metaskills.net' } it 'works' do user_ken.must_be_instance_of User end end ``` ```ruby require 'test_helper' describe User do # This will work too. end ``` Just for reference, here is a full list of each of Rails test case classes and the matching describe alternative if one exists. Remember, names are important when using the describe syntax. So you can not have a mailer named `FooBar` and expect it to work with the outer describe spec style since there is no way to map the spec type based on an existing naming convention. ```ruby # Model Test class UserTest < ActiveSupport::TestCase end describe User do end # Controller Test class UsersControllerTest < ActionController::TestCase end describe UsersController do end # Integration Tests - Must use subclass style! class IntegrationTest < ActionDispatch::IntegrationTest end # Mailer Test class UserMailerTest < ActionMailer::TestCase end describe UserMailer do end # View Helper Test class UsersHelperTest < ActionView::TestCase end describe UsersHelper do end ``` ## Extras We have baked in a few extra methods behind the scenes to minitest-spec-rails. Most directly support our needs to reflect on described classes, however, they may be useful to you too when meta-programming on top of minitest-spec-rails. ### #described_class The `described_class` method is available both via a class method and an instance method in any Rails test case. It is guaranteed to work despite the described level too. This allows class level macros to be build, much like Shoulda. Remember, it can only do this if you follow Rails naming conventions for your tests. ```ruby class UserTest < ActiveSupport::TestCase described_class # => User(id: integer, email: string) it 'works here' do described_class # => User(id: integer, email: string) end describe 'and' do it 'works here too' do described_class # => User(id: integer, email: string) end end end ``` ### mini_shoulda If you are migrating away from Shoulda, then minitest-spec-rails' mini_shoulda feature will help. To enable it, set the following configuration in your test environment file. ```ruby # In config/environments/test.rb config.minitest_spec_rails.mini_shoulda = true ``` Doing so only enables a few aliases that allow the Shoulda `context`, `should`, and `should_eventually` methods. The following code demonstrates the full features of the mini_shoulda implementation. It basically replaces the shell of [should-context](https://github.com/thoughtbot/shoulda-context) in a few lines of code. ```ruby class PostTests < ActiveSupport::TestCase setup { @post = Post.create! :title => 'Test Title', :body => 'Test body' } teardown { Post.delete_all } should 'work' do @post.must_be_instance_of Post end context 'with a user' do should_eventually 'have a user' do # ... end end end ``` If you are in the assertions provided by shoulda-context like `assert_same_elements`, then you may want to consider copying them [from here](https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/assertions.rb) and including them in `MiniTest::Spec` yourself. I personally recommend just replacing these assertions with something more modern. A few examples are below. ```ruby assert_same_elements a, b # From a.sort.must_equal b.sort # To assert_does_not_contain a, b # From a.wont_include b # To ``` ### Matchers If matchers are your thing, I recommend the [minitest-matchers](https://github.com/zenspider/minitest-matchers) gem. For example: ```ruby # Basic minitest-matchers features. describe Post do include ValidAttribute::Method it "must have validations" do post = Post.new post.must have_valid(:title).when("Good") post.wont have_valid(:title).when("") end end # Using an implicit subject. describe Post do subject { Post.new } it { must have_valid(:title).when("Hello") } it { wont have_valid(:title).when("", nil, "Bad") } end ``` Want more? Try out the [valid_attribute](https://github.com/bcardarella/valid_attribute) gem built on top of minitest-matchers. ```ruby describe User do subject { User.new } it { must have_valid(:email).when('test@test.com', 'test+spam@gmail.com') } it { wont have_valid(:email).when('fail', 123) } it { must have_valid(:name).when('TestName') } it { wont have_valid(:name).when('Test') } end ``` ## Gotchas ### Assertion Methods If you are upgrading from Test::Unit, there are a few missing assertions that have been renamed or are no longer available within MiniTest. * The method `assert_raise` is renamed `assert_raises`. * There is no method `assert_nothing_raised`. There are good reasons for this on [Ryan's blog entry](http://blog.zenspider.com/blog/2012/01/assert_nothing_tested.html). ### Mocha If you are using [Mocha](https://github.com/freerange/mocha) for mocking and stubbing, please update to the latest, 0.13.1 or higher so it is compatible with the latest MiniTest. If you do not like the deprecation warnings in older versions of Rails, just add this below the `require 'rails/all'` within your `application.rb` file :) ```ruby require 'mocha/deprecation' Mocha::Deprecation.mode = :disabled ``` ### Rails 3.0.x If you are using minitest-spec-rails with Rails 3.0, then your controller and mailer tests will need to use the `tests` interface for the assertions to be setup correctly within sub `describe` blocks. I think this is a bug with `class_attribute` within Rails 3.0 only. So use the following patterns. ```ruby class UsersControllerTest < ActionController::TestCase tests UsersController end class UserMailerTest < ActionMailer::TestCase tests UserMailer end ``` ## Contributing The minitest-spec-rails gem is fully tested from Rails 3.0 to 4 and upward. We run our tests on [Travis CI](http://travis-ci.org/metaskills/minitest-spec-rails) in both Ruby 1.8 and 1.9. If you detect a problem, open up a github issue or fork the repo and help out. After you fork or clone the repository, the following commands will get you up and running on the test suite. ```shell $ bundle install $ bundle exec rake appraisal:setup $ bundle exec rake appraisal test ``` We use the [appraisal](https://github.com/thoughtbot/appraisal) gem from Thoughtbot to help us generate the individual gemfiles for each Rails version and to run the tests locally against each generated Gemfile. The `rake appraisal test` command actually runs our test suite against all Rails versions in our `Appraisal` file. If you want to run the tests for a specific Rails version, use `rake -T` for a list. For example, the following command will run the tests for Rails 3.2 only. ```shell $ bundle exec rake appraisal:rails32 test ``` Our current build status is: [![Build Status](https://secure.travis-ci.org/metaskills/minitest-spec-rails.png)](http://travis-ci.org/metaskills/minitest-spec-rails)