13

I'm currently going through Michael Hartl's RoR tutorial and am stuck on Chapter 3 when trying to run Spork and Guard. When trying to run tests I get:

/bin/sh: rspec: command not found

Yes, I did look around for an answer but I don't see where the RubyTest.sublime.settings file is so I don't know how to edit it. Can anyone help me out on how to fix my error?

this is my Rubytest.sublime.settings file in my user fodler

{
  "erb_verify_command": "bundle exec erb -xT - {file_name} | ruby -c",
  "ruby_verify_command": "bundle exec ruby -c {file_name}",

  "run_ruby_unit_command": "bundle exec ruby -Itest {relative_path}",
  "run_single_ruby_unit_command": "bundle exec ruby -Itest {relative_path} -n     '{test_name}'",

  "run_cucumber_command": "bundle exec cucumber {relative_path}",
  "run_single_cucumber_command": "bundle exec cucumber {relative_path} -l{line_number}",

  "run_rspec_command": "bundle exec rspec {relative_path}",
  "run_single_rspec_command": "bundle exec rspec {relative_path} -l{line_number}",

  "ruby_unit_folder": "test",
  "ruby_cucumber_folder": "features",
  "ruby_rspec_folder": "spec",

  "ruby_use_scratch" : false,
  "save_on_run": false,
  "ignored_directories": [".git", "vendor", "tmp"],

  "hide_panel": false,

  "before_callback": "",
  "after_callback": ""
}
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • Did you install rspec via rvm or whatever you're using? Are you using Sublime Text 2 to run the tests? – Dave Newton Sep 07 '12 at 20:22
  • @DaveNewton I believe I installed it via rvm (but am not sure. I know rvm is installed on my system though). I'm using sublime text 2, and I'm using the terminal to open my app `subl .`. Do you think it's a $PATH issue? – bigpotato Sep 07 '12 at 20:27
  • Do you execute the rvm shell command in your shell's startup script? Are you using the appropriate gemset? It should be trying to execute the gemset's rvm. If you're running from within ST2 you'd need to set it up to run the appropriate RSpec, not sure how to do that for rspec; making it use the right Ruby is easy. – Dave Newton Sep 07 '12 at 20:35

8 Answers8

18

Now the RubyTest package has a configuration option called "check_for_rvm" that is disabled by default.

You can edit your ~/.config/sublime-text-2/Packages/RubyTest/RubyTest.sublime-settings file and set it to true. This worked for me without doing anything else.

Update: If you are using PackageControl you might need to reinstall the RubyTest package, instead of just updating it.

Fer
  • 3,247
  • 1
  • 22
  • 33
12

The sublime plugin is trying to run the command rspec using shell /bin/sh. However, the command is not found because RVM is not loaded in the shell's environment.

As such, the folder where your rspec executable is located is not in the shell's search path (PATH environment variable). RVM installs any executable commands that come with gems to someplace like: "/home/your-user/.rvm/gems/ruby-1.9.3-p194@myproject/bin/" (actual path depending on your gemset, ruby version, and where your OS stores user home directories)

Simple Solution

As mentioned here... you might find that simply executing sublime from a shell environment containing RVM (ie: your project directory) may solve the PATH problem. However, this requires that you execute your text editor from the command line each time, and that the shell's environment is preserved.

cd ~/src/my-ruby-project
subl .

After much experimentation, I found a way to force the RubyTest plugin to execute rspec with the correct RVM-controlled environment (with bundler support).

With Bundler Support

Here's the contents of my ~/.config/sublime-text-2/Packages/RubyTest/RubyTest.sublime-settings file:

{
  "erb_verify_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/bundle exec erb -xT - {file_name} | ~/.rvm/bin/rvm-auto-ruby -c",
  "ruby_verify_command": "~/.rvm/bin/rvm-auto-ruby -c {file_name}",

  "run_ruby_unit_command": "~/.rvm/bin/rvm-auto-ruby -Itest {relative_path}",
  "run_single_ruby_unit_command": "~/.rvm/bin/rvm-auto-ruby -Itest {relative_path} -n '{test_name}'",

  "run_cucumber_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/bundle exec cucumber {relative_path}",
  "run_single_cucumber_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/bundle exec cucumber {relative_path} -l{line_number}",

  "run_rspec_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/bundle exec rspec {relative_path}",
  "run_single_rspec_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/bundle exec rspec {relative_path} -l{line_number}",

  "ruby_unit_folder": "test",
  "ruby_cucumber_folder": "features",
  "ruby_rspec_folder": "spec",

  "ruby_use_scratch" : false,
  "save_on_run": false,
  "ignored_directories": [".git", "vendor", "tmp"],

  "hide_panel": false,

  "before_callback": "",
  "after_callback": ""
}

This should work as long as you've got bundler in your global gemset, and RVM installed to your home dir (adjust paths as needed if ~/.rvm does not evaluate correctly, or if bundler or rvm-auto-ruby is located somewhere else).

If you are using gemsets you should also add a line like the following to your project's .rvmrc file:

rvm use ruby-1.9.3-p327@your_project_gemset_name

Without Bundler Support

This assumes you have cucumber and rspec installed to the @global gemset of your current ruby:

{
  "erb_verify_command": "~/.rvm/bin/rvm-exec $(~/.rvm/bin/rvm current) 1>/dev/null erb -xT - {file_name} | ~/.rvm/bin/rvm-auto-ruby -c",
  "ruby_verify_command": "~/.rvm/bin/rvm-auto-ruby -c {file_name}",

  "run_ruby_unit_command": "~/.rvm/bin/rvm-auto-ruby -Itest {relative_path}",
  "run_single_ruby_unit_command": "~/.rvm/bin/rvm-auto-ruby -Itest {relative_path} -n '{test_name}'",

  "run_cucumber_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/cucumber {relative_path}",
  "run_single_cucumber_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/cucumber {relative_path} -l{line_number}",

  "run_rspec_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/rspec {relative_path}",
  "run_single_rspec_command": "~/.rvm/bin/rvm-auto-ruby $(~/.rvm/bin/rvm gemdir | sed -e 's/@.*//' -e 's/$/@global/' )/bin/rspec {relative_path} -l{line_number}",

  "ruby_unit_folder": "test",
  "ruby_cucumber_folder": "features",
  "ruby_rspec_folder": "spec",

  "ruby_use_scratch" : false,
  "save_on_run": false,
  "ignored_directories": [".git", "vendor", "tmp"],

  "hide_panel": false,

  "before_callback": "",
  "after_callback": ""
}
Community
  • 1
  • 1
TrinitronX
  • 4,959
  • 3
  • 39
  • 66
5

I spent many hours struggling with this same problem! I could not get rspec to run within Sublime Text 2, using the Michael Hartl "Ruby on Rails Tutorial." It kept saying

/bin/sh: rspec: command not found

I finally realized that the RubyTest package was looking in the WRONG PLACE for my RVM!

On my Mac, the path for RubyTest is /Library/Application Support/Sublime Text 2/Packages/Ruby Test

First, to make RubyTest seek the RVM, I changed the parameter in RubyTest.sublime-settings from

"check_for_rvm": false, to "check_for_rvm": true,

Then I dug into the Python code of run_ruby_test.py.

At line 151, inside class BaseRubyTask, it had the wrong path for my RVM:

rvm_cmd = os.path.expanduser('~/.rvm/bin/rvm-auto-ruby')

I changed it to the full correct path:

rvm_cmd = os.path.expanduser('/usr/local/rvm/bin/rvm-auto-ruby')

If this is not your path, find the correct path by typing which rvm-auto-ruby and substitute that instead.

After saving run_ruby_test.py, I went to Terminal, cd to my Rails application directory, and ran spork

Finally, I opened static_pages_spec.rb in Sublime Text 2. Now all the tests work from it!

Raymond Gan
  • 4,612
  • 3
  • 24
  • 19
0

From the terminal, try 'gem list -d rspec'. That should print out any gems you have w/ rspec in the name. If there aren't any, try 'gem install rspec'.

Assuming that, from terminal, you do have rspec installed (which you can verify with "which rspec", etc.), in that case your problem is likely to do with how Sublime is configured. Note the path where rspec is installed and check whether your editor has that in its paths.

(Sorry, I'm not familiar with Sublime in particular.)

Benissimo
  • 1,010
  • 9
  • 14
0

Refer to the Bundler settings: https://github.com/maltize/sublime-text-2-ruby-tests

About RubyTest.sublime-settings, it should be under your home folder somewhere in sublime-test2/Packages/RubyTest folder. In the case of Ubuntu it would be:

~/.config/sublime-text-2/Packages/RubyTest
prusswan
  • 6,853
  • 4
  • 40
  • 61
  • hi. now i get `/bin/sh: bundle: command not found` after copying the `RubyTest.sublime-settings` file to the User folder and then adding `bundle exec` to each command. – bigpotato Sep 07 '12 at 21:23
  • @Edmund did you follow the book's recommendation on rvm setup? Are you able to execute `bundle exec rspec spec` in the terminal window? – prusswan Sep 07 '12 at 21:25
  • @Edmund that would be unusual since you are already starting it with `subl`, perhaps this will be helpful: https://github.com/maltize/sublime-text-2-ruby-tests/issues/75 – prusswan Sep 07 '12 at 21:31
0

You need to start sublime from terminal within the working directory: example - (goto working directory in terminal, then start sublime from terminal )

cd rails/my_app_dir
subl .

0

For anybody using rbenv, there is also this setting in rubyTest.sublime-settings which will do the trick:

"check_for_rbenv": true
Ben
  • 695
  • 6
  • 12
0

if you've used rbenv during installation of Ruby in Mac and have issue with running "rspec" after installation "rspec gem", like: "-bash: rspec: command not found", just refresh it by typing command:

rbenv rehash
sambua
  • 2,274
  • 3
  • 22
  • 20