Convert Test::Unit to RSpec by Script

Living with Test and Spec at the same time is annoying, so here is a small Howto for conversion using the Test::Unit to RSpec converter.

  1. Change spec/spec_helper.rg ‘config.fixture_path =‘ to test/fixtures OR copy all fixtures from test to spec (svn cp test/fixtures spec/fixtures)
  2. copy old code from test_helper to spec_helper (leave includes outside of Spec::Runner.configure do…)
  3. sudo gem install spec_converter
  4. convert all tests ‘spec_converter
  5. correct syntax errors
  6. search and replace ‘test/xxx’ with ‘spec/xxx’ where neccessary
  7. run ‘rake spec
  8. dance if result == :success

Works very nice for me, just one syntax error to correct, and then everything runs. Not all old asserts will be replaced, but this is not problem, since RSpec can work with Test::Unit assertions.

2 thoughts on “Convert Test::Unit to RSpec by Script

  1. Thanks!

    I also did this:
    – move functionals to controllers
    – move units to models
    – rename files from _test to _spec
    – replace all should.not to should_not
    – (syntax varies for this one:) describe “Foo”, ActionController::TestCase do becomes describe Foo do
    before do => before(:each) do
    – “should = ” becomes “should ==”
    – “assert_tag” becomes “response.should have_tag”

    Also, I had an issue with assert_tag in controller tests that were looking for XML. But really the auto converted controller tests are not very RSpec-ish.

  2. In order to get assert_tag and other view-dependent code working, add “integrate_views” to the top of your spec (after describe FooController do). This will make RSpec run the views instead of stubbing them out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s