RSpec 书 - 'require': 无法加载此类文件 -- codebreaker (LoadError)

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

实用程序员 RSpec 书中的练习是编写一个名为 Codebreakers 的问题解决游戏。目的是使用 Cucumber 和 Rspec in Ruby 接触 BDD。

这是我的代码破解者目录树的概述:

-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

codebreaker 的 LoadError 消息:

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 要求 '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 需要 'codebreaker',根据错误未加载:

require 'codebreaker'

而 lib/codebreaker/codebreaker.rb 需要 codebreaker/game

require 'codebreaker/game'

lib/codebreaker/game.rb:

module Codebreaker
  class Game
    def initialize(output)
    end

    def start
    end
  end
end

欢迎提出建议和见解!

谢谢。

我认为您需要将 spec_helper.rb 中的要求更改为

require 'codebreaker/codebreaker'

因为你的 lib 目录树看起来像这样

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