The Trouble with RSpec

Recently, I’ve been muddling through 10 different tutorials on RSpec, a tool for behavior-driven development in Ruby.  After a few days, I finally got it to work.  Here are a few tips if you’re having trouble getting it started:

  1. Make sure you run “rspec –init” in your project directory first.  This will create a “spec” folder with your “spec_helper”.
  2. Make sure that everything requires the appropriate files.  For example, if you’re making a program called “hello.rb”, then it should as follows:
    • “hello_spec.rb” should require “spec_helper.rb”
    • “spec_helper” should require “hello.rb”

The point of spec_helper is just to require everything you need in one fell swoop.  For now, it only requires one file, but if the program gets more complex, it could easily require several.

Another point is to make sure you use “require_relative”.  If you open spec_helper, it should have this line:

       require_relative “../hello”

The “../” means “go up one directory”.

Finally, if you use the “rake” task, you must make sure the path to your project doesn’t contain any directories with spaces in their names.  For example, I put my projects in a folder called “Web Development” and I found I had to change it to “Web_Development” in order to have “rake” work properly.

 

Leave a comment