16

Noob who may be missing something obvious ... I'm trying to debug an Rspec file. The Rspec file is stripped down at this point:

  require 'spec_helper'

  describe PagesController do

    render_views

    describe "GET 'home'" do
        describe "when not signed in" do

        before(:each) do
          get :home
        end

        it "should be successful" do
          response.should be_success
        end

        it "should have a vendor section" do
          response.should have_selector("h1", :content => "Vendor")
        end

        it "should have a hospital section" do
          response.should have_selector("h1", :content => "Hospital")
        end
    end
  end

I make the following call from the command line:

rdebug spec/controllers/pages_controller_spec.rb

The debugger runs, but throws the following error:

> require 'spec_helper'

    <internal:lib/rubygems/custom_require>:29:in `require'
    <internal:lib/rubygems/custom_require>:29:in `require'
    /home/kevin/.rvm/bin/rails_projects/evaluationrx/spec/controllers/pages_controller_spec.rb:1:in `<top (required)>'
    /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_load'
    /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_program'
    /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:412:in `<top (required)>'
    /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/bin/rdebug:19:in `load'
    /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/bin/rdebug:19:in `<main>'
Uncaught exception: no such file to load -- spec_helper

Rspec without the debugger without a problem. I'm using Rspec 2.3.0, ruby-debug19 (0.11.6), Rails 3.0.3 and ruby 1.9.2. Why can't the debugger see the spec_helper file?

Kevin
  • 744
  • 2
  • 7
  • 17

2 Answers2

41

Make sure you've run

rails generate rspec:install

to create the spec_helper.rb file.

Jason Galvin
  • 1,313
  • 1
  • 12
  • 10
  • I had to put this in my gem file for this command to work `group :development, :test do gem 'rspec-rails', ">= 2.0.0.beta" end` – Leo Apr 26 '12 at 18:16
23

I assume your spec_helper.rb resides in the spec directory? Try:

require_relative '../spec_helper'
rausch
  • 3,148
  • 2
  • 18
  • 27
  • You're awesome. Yes, that works. Why is require_relative needed in this situation? – Kevin Feb 18 '11 at 19:57
  • 4
    `require` searches, amongst other paths, in your current directory, `require_relative` searches relative to the file you put it in. The old way would have been something like `require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")`. So somebody came up with the more handy `require_relative`. – rausch Feb 19 '11 at 10:22