Last Updated: September 09, 2018
·
3.027K
· Marko Klemetti

Sublime, RVM and RubyTest

It took me a while to solve how to best run RSpec (and other tests) while using RVM in Sublime.

My requirements for the functionality

  • One shortcut for all tests in the current file
  • One shortcut for the current test (where the cursor is)
  • Using the same independent environment as in my shell
  • Test results visible in Sublime (not in external terminal creen)

Installing RVM

RVM is easily the best tool to keep your ruby versions and gemsets separated. To install RVM, just run

$ curl -L https://get.rvm.io | bash -s stable --ruby

Now you can use rvm-auto-ruby, a tool that sets up your shell with the correct ruby version and gemset:

$ ~/.rvm/bin/rvm-auto-ruby -S <your ruby command>

Install RubyTest in Sublime

If you have not yet installed Package Control for Sublime, do it now.

With Package Control you can install new packages by pressing Shift-Command-P and selecting Package Control: Install Package (or just type "ins" and hit enter).

Once the available packages have been loaded, find RubyTest and press enter.

Configure RubyTest

Once you have installed RubyTest, you can change the configurations in Sublime Test 2 -> Preferences -> Package Settings -> RubyTest. Pick Settings - User and add the following configuration for RSpec:

{
  "run_rspec_command": "~/.rvm/bin/rvm-auto-ruby -S rspec {relative_path}",
  "run_single_rspec_command": "~/.rvm/bin/rvm-auto-ruby -S rspec {relative_path} -l{line_number}"
}

If you are using Test::Unit or Cucumber, pick the configurations from Settings - Default change the configurations accordingly.

Additionally you can change the default keyboard shortcuts in Key Bindings - User:

[
  { "keys": ["super+shift+t"], "command": "run_single_ruby_test" }, // single test
  { "keys": ["super+shift+r"], "command": "run_all_ruby_test" }, // test file
]

Always remember to make your Sublime settings in User, not in Default. Otherwise your settings might get overridden when the package is updated.

Runing tests in Sublime

Press Shift-Command-R and voila:

..

Finished in 0.00056 seconds
2 examples, 0 failures
[Finished in 1.5s]