Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Minitest in Rails

Tags:

Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are

a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest),
b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it's used more with gems/libraries).

Is it worth using minitest when rspec has the main dominance with testing rails applications?

like image 470
John Smith Avatar asked Oct 02 '11 19:10

John Smith


People also ask

How do you run a Minitest in Rails?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.

Does Minitest come with Ruby?

In this post, we will talk about Minitest, the standard software testing framework provided with Ruby. It isn't the only software testing framework available, but being supplied automatically with Ruby is a major advantage. In particular, we will discuss how to use Minitest assertions to test your software.

How do I run a test file in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.


2 Answers

I'm the author of minitest-rails. Things have changed a lot from the time you originally asked this to now. My answer assumes you're using minitest-rails.

Named Routes

If you are using minitest-rails this just works (now). You can use the generators to create these tests, or write them yourself. All the named routes are available in your acceptance/integration tests.

require "minitest_helper"

describe "Homepage Acceptance Test" do
  it "must load successfully" do
    get root_path
    assert_response :success
  end
end

Adoption

I think we will continue to see increased attention on using Minitest with Rails as we get closer to Rails 4.

Worth it?

I think starting with Minitest now is totally worth it. There is tremendous activity going on in Minitest right now. It aligns nicely with the recent focus on fast tests as well. But it really depends on your app and team dynamics.

like image 172
blowmage Avatar answered Sep 28 '22 01:09

blowmage


I recently switched an application from Rspec to Minitest & it was well worth it. Tests run much faster, the syntax encourages smarter, leaner code, & somehow I just have more confidence in the suite now (less magic at work).

The improvement extends to integration/acceptance testing, I find Minitest with Capybara much more readable & straightforward than Cucumber (& much less brittle).

Below is a helper file that should be all you need to get unit, functional & integration tests running with Minitest using spec syntax. This was based on a gist by @tenderlove & a lot of reading/experimentation. Notes & caveats below.

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

require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require 'action_controller/test_case'

require 'miniskirt'
require 'capybara/rails'
require 'mocha'
require 'turn'

# Support files
Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
  require file
end


class MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown

  alias :method_name :__name__ if defined? :__name__
end


class ControllerSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include ActionController::TestCase::Behavior

  before do
    @routes = Rails.application.routes
  end
end

# Test subjects ending with 'Controller' are treated as functional tests
#   e.g. describe TestController do ...
MiniTest::Spec.register_spec_type( /Controller$/, ControllerSpec )


class AcceptanceSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL

  before do
    @routes = Rails.application.routes
  end
end

# Test subjects ending with 'Integration' are treated as acceptance/integration tests
#   e.g. describe 'Test system Integration' do ...
MiniTest::Spec.register_spec_type( /Integration$/, AcceptanceSpec )


Turn.config do |c|
  # use one of output formats:
  # :outline  - turn's original case/test outline mode [default]
  # :progress - indicates progress with progress bar
  # :dotted   - test/unit's traditional dot-progress mode
  # :pretty   - new pretty reporter
  # :marshal  - dump output as YAML (normal run mode only)
  # :cue      - interactive testing
  c.format  = :cue
  # turn on invoke/execute tracing, enable full backtrace
  c.trace   = true
  # use humanized test names (works only with :outline format)
  c.natural = true
end

Notes

  • Geared for use in Rails 3.1 or 3.2. Haven't tried below that.
  • gem 'minitest' is necessary to get some more advanced Minitest functionality (let blocks, etc.)
  • This uses mocha (fuller mocks/stubs), miniskirt (factory_girl lite), & the new turn runner. None of these are dependencies.
  • As of Rails 3.2, nested describe blocks in controller tests throw an error
like image 45
Seth Bro Avatar answered Sep 28 '22 02:09

Seth Bro