Lugo Labs

Run single tests in Ruby on Rails 4

The way to run single tests on pre Ruby on Rails 4 is with the command:

sh
bundle exec rake test TEST=test/models/book_test.rb

The unfortunate bit about the command is that there's not hints in the shell, we need to type the file path correctly. Or as I do, perform a ls first, then copy and paste the path.

I have now changed this process by using a shell function (I use oh-my-zsh, so I've added it to my ~/.zshrc file):

sh
function tst() {
  eval "bundle exec rake test TEST=$1"
}

Now we can call:

sh
tst test/models/book_test.rb

and take full advantage of the shell hints. No more mis-typing file paths, or copy and pasting them.

Rails 5 corrects this process, and you can just run:

sh
bin/rails test test/models/book_test.rb