Search code examples
rubyrspeccucumberbdd

The RSpec Book - 'require': cannot load such file -- codebreaker (LoadError)


The exercise in the Pragmatic Programmers' RSpec Book is to write a problem solving game called Codebreakers. The purpose is to get exposure to BDD using Cucumber and Rspec in Ruby.

Here's an overview of my directory tree for codebreaker:

-features
  -step_definitions
  -codebreaker_steps
  -support
    -env.rb
  -codebreaker_starts_game.feature
  -codebreaker_submits_guess.feature
-lib
  -codebreaker
    -codebreaker.rb
    -game.rb
-spec
  -codebreaker
    -game_spec
  -spec_helper.rb

The LoadError Message for codebreaker:

rspec spec/codebreaker/game_spec.rb 
/Documents/rubyProjects/codebreaker/spec/spec_helper.rb:1:in `require': cannot load such file -- codebreaker (LoadError)
    from /Documents/rubyProjects/codebreaker/spec/spec_helper.rb:1:in `<top (required)>'
    from /Documents/rubyProjects/codebreaker/spec/codebreaker/game_spec.rb:1:in `require'
    from /Documents/rubyProjects/codebreaker/spec/codebreaker/game_spec.rb:1:in `<top (required)>'

spec/codebreaker/game_spec.rb requires 'spec_helper':

require 'spec_helper'

  module Codebreaker
    describe Game do
      describe "#start" do
        it "sends a welcome message"
        it "prompts for the first guess"
      end
    end
  end

spec/spec_helper.rb requires 'codebreaker', which is not loading as per the error:

require 'codebreaker'

And lib/codebreaker/codebreaker.rb requires codebreaker/game

require 'codebreaker/game'

lib/codebreaker/game.rb:

module Codebreaker
  class Game
    def initialize(output)
    end

    def start
    end
  end
end

Suggestions and insight is appreciated!

Thank you.


Solution

  • I believe you need to change your require in your spec_helper.rb to be this

    require 'codebreaker/codebreaker'
    

    since you your lib directory tree looks like this

    -lib
      -codebreaker
        -codebreaker.rb
        -game.rb