Search code examples
ruby-on-rails-3.2rspec-railstestunit

Rails 3.2: Switching from Rspec to Test::Unit


I created a new Rails 3.2.x application with the -T option for no tests because I wanted to use Rspec for testing.

Now I want to revert it back to Test::Unit. How can I do that such that all of the rake tasks work, and new scaffolds are generated with Test::Unit instead of RSpec test shells?


Solution

  • After searching and hacking, here is what got it to work for me.

    1. Remove rspec and rspec-rails from the Gemfile
    2. Run "gem list rspec --local", and do a "gem uninstall" for each of those rspec gems.
    3. Delete Gemfile.lock
    4. rm -r spec/
    5. bundle install
    6. mkdir test/
    7. Add "require 'rails/all'" to config/application.rb
    8. Create a file test/test_helper.rb and put the following code in it:

      ENV["RAILS_ENV"] = "test"
      require File.expand_path('../../config/environment', __FILE__)
      require 'active_support'
      require 'rails/test_help'
      # require 'factory_girl_rails'  #use if you are using FactoryGirl for fixtures
      
      class ActiveSupport::TestCase
        # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
        #
        # Note: You'll currently still have to declare fixtures explicitly in integration tests
        # -- they do not yet inherit this setting 
        fixtures :all
      
        # Add more helper methods to be used by all tests here...
      end
      

    After that, you can generate tests using

    rails g integration_test SomeStories
    

    Or you can just add unit tests under test/unit and then run them with

    rake test
    rake test:recent