Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Created February 6, 2017 17:30
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mariochavez/c21dfdf564dd47e65e44e8adb8243d28 to your computer and use it in GitHub Desktop.
Save mariochavez/c21dfdf564dd47e65e44e8adb8243d28 to your computer and use it in GitHub Desktop.
RSpec to Minitest

These are the scripts that I used to migrate from RSpec to Minitest in a Rails aplication.

It is expected for the test suite to be in RSpec 3.x sintax.

  1. Install rename library from Homebrew
  2. Remove RSpec/TestUnit from Gemfile
  3. Add minitest-rails gem
  4. Rename spec forder to test
  5. Add a test_helper.rb file
ENV["RAILS_ENV"] = "test"

require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"

# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
# require "minitest/rails/capybara"

# Uncomment for awesome colorful output
# require "minitest/pride"

Dir[Rails.root.join("test/support/**/*.rb")].each { |file| require file }

module ActiveSupport
  class TestCase
    # Setup all fixtures in test/fixtures/*.yml for all tests in
    # alphabetical order.
    fixtures :all
    # Add more helper methods to be used by all tests here...
  end
end

  1. Rename all spec files with
find . -name *_spec.rb -exec rename 's|_spec|_test|' {} \;
  1. Change sintax to Minitest
find . -name '*_test.rb' -exec sed -ie 's/context/describe/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/stub(/stubs(/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/double/stub/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/should_receive/expects/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/and_return/returns/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/expect(//g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to be ==/.must_equal/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to eq/.must_equal/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to be_empty/.must_be_empty/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to_not be_empty/.wont_be_empty/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to be_kind_of/.must_be_kind_of/g' {} \;
find . -name '*_test.rb' -exec sed -ie ’s/(response).to have_http_status/.must_respond_with/g’ {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to redirect_to/.must_redirect_to/g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/RSpec.//g' {} \;
find . -name '*_test.rb' -exec sed -ie 's/).to match/.must_match/g' {} \;
  1. Integration tests name muest end with Integration Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment