I’m writing a little mini Rails app (my first one) and I’ve been working with RSpec for other Ruby scripts I’ve been doing so far. However, in my ignorance I did not realize that rails new <app> would generate the standard Rails tests (well, duh! ;] ). So now I’d like to switch to RSpec. Here are the steps I went through:
- Follow the installation instructions at https://github.com/rspec/rspec-rails
- Generate test stubs for all models/views/controllers, etc. (Warning: this will try to generate controllers for EVERY directory under app, some of which don’t have RSpec generators – could be optimized, but good enough for me right now. For example, if you use this in Rails 4, it will try to generate a
concerns
model spec, which is actually a directory, so I just deleted that one afterwards.)
for dirs in app/*; do
dir=$(basename $dirs | sed 's/s$//')
echo $dir
for file in $(find ${dirs}/* -type f -not -name "_*"); do
rails generate rspec:${dir} ${$(basename ${file%.*} | sed 's/_'${dir}'//')%.*}
done
done
-
RAILS_ENV=test rake db:migrate
to make sure your test DB is up to date (this shouldn’t be necessary if you’ve been writing tests – you have been writing tests, right? ;] ) - Delete the old
test
directory. - Run
rspec
, make sure everything works - Profit!
Note that of course I didn’t have any tests written yet (*ahem*) – if you do, this method will NOT work for you.
There you have it. To quote Avdi Grimm: happy hacking!