Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Test::Unit to Rspec conversion ruby script
require "highline/import"
from_path = ask "Enter path for file to convert: "
to_path = ask "Enter the path where to save the spec file: "
unless File.exists? from_path
puts "Sorry this file doesn't exist!: #{from_path}"
puts "Please try again"
t_unit =
t_unit.gsub! /require 'test_helper'/, "require 'spec_helper'"
t_unit.gsub! /class (\w+)Test < ActionController::TestCase/, 'describe \1 do'
t_unit.gsub! /class (\w+)Test < ActiveSupport::TestCase/, 'describe \1 do'
t_unit.gsub! /class (\w+)Test < ActionDispatch::IntegrationTest/, 'describe \1 do'
t_unit.gsub! /context/, "describe"
t_unit.gsub! /test ["|']([\w|\s]+)["|'] do/, 'it "\1" do'
t_unit.gsub! /setup do/, "before do"
t_unit.gsub! /def setup/, "before do"
t_unit.gsub! /should "(.+)" do/, 'it "\1" do'
t_unit.gsub! /should respond_with :(\w+)/, 'it {response.should be_\1}'
t_unit.gsub! /should assign_to\(:(\w+)\).with\((.+)\)/, 'it {assigns(:\1).should eql \3}'
t_unit.gsub! /should assign_to :(\w+)/, 'it {assigns(:\1).should_not be_nil}'
t_unit.gsub! /should assign_to\((.+)\).with_kind_of\((.+)\)/, 'it {assigns(\1).should be_a(\2)}'
t_unit.gsub! /should render_template (.+)/, 'it {response.should render_template(\1)}'
t_unit.gsub! /should_not set_the_flash/, 'it {flash.should be_empty}'
t_unit.gsub! /assert_equal ([\w|\.|\(|\)|\d].+),\s(.+)/, '\1.should eql \2'
t_unit.gsub! /should set_the_flash(\.now)??\.to[\s|\(][\s|\(]?(.+)\)/, 'it {flash\1[:notice].should =~ \2}'
t_unit.gsub! /should redirect_to\((.+)\)\{(.+)\}/, 'it {response.should redirect_to(\2)}'
t_unit.gsub! /should redirect_to[\s+|\(]@(\w+)[\)]/, 'it {response.should redirect_to(@\1)}'
t_unit.gsub! /(\s+)should redirect_to\((.+)\) /, '\1it {response.should redirect_to(\2)}'
t_unit.gsub! /flash(\.now)??\[:(\w+)\].should =~ "([\w|\s|\.]+)"/, 'flash\1[:\2].should =~ /\3/'
t_unit.gsub! /it \{assigns\(:(\w+)\)\.should eql \}/, 'it {assigns(:\1).should eql @\1}'
t_unit.gsub! /(\w+).count-@(\w+)_count\.should/, '(\1.count-@\2_count).should'
t_unit.gsub! /it \{flash\[:notice\]\.should =~ \/(\w+) not updated\/\}/, 'it {flash[:alert].should =~ /\1 not updated/}'
t_unit.gsub! /it \{flash\[:notice\]\.should =~ \/(\w+) not created\/\}/, 'it {flash[:alert].should =~ /\1 not created/}'
t_unit.gsub! /should belong_to :(\w+)/, 'it {should belong_to :\1}'
t_unit.gsub! /should have_many :(\w+)/, 'it {should have_many :\1}'
t_unit.gsub! /should validate(.+)/, 'it {should validate\1}'
t_unit.gsub! /assert page.has_selector\?/, 'page.should have_selector'
t_unit.gsub! /assert page.has_content\?/, 'page.should have_content'
t_unit.gsub! /Factory :(\w+)/, 'FactoryGirl.create :\1'
t_unit.gsub! /Factory\(:(\w+)/, 'FactoryGirl.create(:\1'
rspec = to_path, "w"
rspec.puts t_unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment