Skip to content

Instantly share code, notes, and snippets.

@monicao
Last active June 22, 2021 16:55
Show Gist options
  • Save monicao/9595465 to your computer and use it in GitHub Desktop.
Save monicao/9595465 to your computer and use it in GitHub Desktop.
Setting up rails 4 with MiniTest, Fabrication and Guard
# Gemfile
group :development, :test do
  gem 'minitest-rails'
  gem 'fabrication'
end
bundle install

Update test/test_helper.rb with the minitest settings.

bin/rails generate minitest:install

Set up the rails generators to use minitest and fabrication

# config/application.rb
# Use Minitest for testing. Fabrication instead of fixtures.
config.generators do |g|
  g.test_framework      :minitest, spec: true, fixture_replacement: :fabrication
  g.fixture_replacement :fabrication, dir: "test/fabricators"
end
$ bin/rails g model team name:string
      invoke  active_record
      create    db/migrate/20140317075017_create_teams.rb
      create    app/models/team.rb
      invoke    mini_test
      create      test/models/team_test.rb
      invoke      fabrication
      create        test/fabricators/team_fabricator.rb

The generated model test case should looks something like this

# test/models/team_test.rb
require "test_helper"

describe Team do
  before do
    @team = Team.new
  end

  it "must be valid" do
    @team.valid?.must_equal true
  end
end

Take it for a spin

$ bin/rake test
Run options: --seed 37431

# Running tests:

.

Finished tests in 0.442097s, 2.2619 tests/s, 2.2619 assertions/s.

Install Capybara for integration tests.

group :development, :test do
  gem 'minitest-rails-capybara'
end

To generate an integration test with the spec syntax use:

bin/rails generate minitest:feature CanAccessHome --spec

TODO: Find a way to make spec the default syntax for integrationt tests.

Install Guard to get +2 awesomeness.

# Gemfile
group :development do
  gem 'guard-minitest'
end

Generate the sample Guardfile

bundle exec guard init minitest

Enable the Rails 4 settings. Note: Using MiniTest::Spec but the test file names are the MiniTest::Unit names. ex: test/mymodel_test.rb.

Don't run all tests automatically when guard starts. This can take a while.

# Guardfile
guard(:minitest, all_on_start: false,
      all_after_pass: false,
      notification: true) do
  # with Minitest::Unit
  # watch(%r{^test/(.*)\/?test_(.*)\.rb$})
  # watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  # watch(%r{^test/test_helper\.rb$})      { 'test' }

  # with Minitest::Spec
  # watch(%r{^spec/(.*)_spec\.rb$})
  # watch(%r{^lib/(.+)\.rb$})         { |m| "spec/#{m[1]}_spec.rb" }
  # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }

  # Rails 4
  watch(%r{^app/(.+)\.rb$})                               { |m| "test/#{m[1]}_test.rb" }
  watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
  watch(%r{^app/controllers/(.+)_controller\.rb$})        { |m| "test/integration/#{m[1]}_test.rb" }
  watch(%r{^app/views/(.+)_mailer/.+})                   { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
  watch(%r{^lib/(.+)\.rb$})                               { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^test/.+_test\.rb$})
  # watch(%r{^test/test_helper\.rb$}) { 'test' }

end

Try it out

bundle exec guard
be guard
15:38:00 - INFO - Guard is using TerminalTitle to send notifications.
15:38:00 - INFO - Guard::Minitest 2.2.0 is running, with Minitest::Unit 4.7.5!
15:38:00 - INFO - Running: all tests
Run options: --seed 24541

# Running tests:

.

Finished tests in 0.042472s, 23.5449 tests/s, 23.5449 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

15:38:03 - INFO - Guard is now watching at '/Users/momo/Code/myproject'
[1] guard(main)>

Set up guard to not run the entire test suite when it starts

# Guardfile
guard(:minitest, all_on_start: false,
      all_after_pass: false,
      notification: true) do
end

@arkhamRejek
Copy link

you rock!

@raecoo
Copy link

raecoo commented Jan 18, 2016

Thanks sharing.

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