Run single test with Spring in Rails 4.1
In the old days, you would run a single test with:
$ ruby -I test test/functional/my_controller_test.rb -n test_my_case
This is all good and well, but is painfully slow. Then there were brilliant people who created Spork, Zeus and Spring to speed up that process.
With version 4.1 Rails adopted Spring as the official preloader. Probably due to its pure Ruby nature. Unfortunately it is the only one of the pack that does not seem to support the single test usage.
After munging around I discovered a method that can be used for any preloader. By putting a script script/test.rb:
# script/test.rb $: << Rails.root.join('test') options = {} OptionParser.new do |opts| opts.on('-n TEST_NAME') do |n| options[:test_name] = n end opts.on('-e ENVIRONMENT') do |e| raise ArgumentError.new("Must run in test environment") if e != 'test' end end.parse! test_files = ARGV.dup ARGV.clear if options[:test_name] ARGV << "-n" << options[:test_name] end test_files.each do |f| Dir[f].each do |f1| load f1 end end
Usage is just like good old ruby -I test
:
$ bin/rails r -e test script/test.rb test/controllers/my_controler_test.rb -n test_my_case $ bin/rails r -e test script/test.rb test/controllers/**/*.rb
If that’s too long, you can just do something like alias rt=bin/rails r -e test script/test.rb
in your shell’s rc