8

I'm trying to run the basic starter example for using rspec found here: http://rspec.info/.

When I type in the command prompt

ruby bowling_spec.rb

I get the following error

Test

# bowling_spec.rb
require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should == 0
  end
end

Code

# bowling.rb
class Bowling
  def hit(pins)
  end

  def score
    0
  end
end

Error Message

internal:lib/rubygems/custom_require:29:in require': no such file to load -- bowling (LoadError) from <internal:lib/rubygems/custom_require>:29:inrequire' from bowling_spec.rb:2:in `'

John
  • 4,362
  • 5
  • 32
  • 50
  • Now that I think about it, this looks to be a duplicate of [Neither `ruby` and nor `irb` can load `.rb` file in current directory](http://StackOverflow.Com/q/3898174/), [Why isn't current directory on my Ruby path?](http://StackOverflow.Com/q/4965556/), [Ruby require 'file' doesn't work but require './file' does. Why?](http://StackOverflow.Com/q/3795513/), [Ruby Strange Error](http://StackOverflow.Com/q/4005013/) and probably a couple of others. – Jörg W Mittag Feb 22 '11 at 04:20
  • @Joerg: The answer that got accepted wouldn't be applicable to the other two questions, so I don't think it's a duplicate. – Andrew Grimm Feb 22 '11 at 06:35

4 Answers4

16

This is a great simple example to get started with rspec. To make everything work do the following:

  1. Place your bowling.rb file in lib/bowling.rb.
  2. Place your bowling_spec.rb file in spec/bowling_spec.rb.
  3. Run the command rspec spec/bowling_spec.rb if you are using rpsec 2.
  4. Run the command spec spec/bowling_spec.rb if you are using rspec 1.

Also, an updated example can be found here.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
  • This implementation was also a solution, and was the most complete/simplest solution. – John Feb 22 '11 at 04:21
4

In your bowling_spec.rb, change

require 'bowling'

to

require './bowling'

ruby 1.9 loads paths differently from ruby 1.8 and no longer adds the current directory by default

noli
  • 15,927
  • 8
  • 46
  • 62
  • Making this change along with using rspec instead of ruby in the command prompt solved this issue. – John Feb 22 '11 at 04:19
  • Thanks noli, a much better introductory example here https://www.relishapp.com/rspec/docs/gettingstarted. Debugging a basic example can be frustrating when you're starting something new, you expect for rspec's official introduction to work. – Dru Mar 27 '12 at 14:51
2

As a general hint for solving such error messages: in 99% of all cases, when a computer tells you it cannot find something, it is because that thing doesn't exist. In 99% of the rest of the cases, that thing isn't where the computer expects it to be.

The first one is obviously not the case here, since bowling.rb clearly does exist. However, your problem squarely falls into those second 99%: require loads a file file from Ruby's $LOAD_PATH. But you didn't put bowling.rb into a directory on Ruby's $LOAD_PATH, you simply put it in the same directory as bowling_spec.rb.

One obvious solution would be to add that directory to Ruby's $LOAD_PATH. Obvious, but – in this case – wrong.

If you want to load a file relative to the position of the currently executing file, you need to use require_relative. This is the right solution in this case.

Note that, in order for the describe method to even exist, let alone work, you would also need to either add require 'rspec' to your spec file, or run it using the RSpec runner (i.e. rspec bowling_spec.rb instead of ruby bowling_spec.rb), but this is completely unrelated to the problem you are seeing.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • In frameworks like RSpec or Rails, isn't it more likely that you forgot to supply the fairy dust? (I wish I was joking) – Andrew Grimm Feb 22 '11 at 22:24
1

First, from the example it looks like you need to run rspec bowling_spec.rb or spec bowling_spec.rb otherwise the "describe" methods used for the spec won't be loaded and you'll get some other errors.

Next, are the files both in the same directory? They need to be, with the way you have the require line setup. Other than that I think it looks ok.

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71