Last Updated: April 19, 2017
· matenia

Mass Replace "spec_helper" with "rails_helper" in spec/ folder

Upgrading from RSpec 2.x to RSpec 3.x needs a few structural changes.
This quick script will search the spec/ folder for any file that has a line that contains both require and spec_helper and replace spec_helper with rails_helper.

It's enough to do most of the spec file changes required but you should modify to suit your needs and ALWAYS check your git diff before committing.


# get a list of files which contain "require" AND "spec_helper" on a single line
# excluding spec/rails_helper.rb (we want "require 'spec_helper'" to exist here)
declare -a files=$(git grep -E "require.*spec_helper" | cut -d ':' -f 1 | grep -v 'spec/rails_helper.rb')

# loop through files and replace "spec_helper" with "rails_helper"
for i in $files; do
  sed -i '' -e 's/spec_helper/rails_helper/' $i