【问题标题】:The RSpec Book - 'require': cannot load such file -- codebreaker (LoadError)RSpec Book - 'require':无法加载此类文件 - 代码破解器(LoadError)
【发布时间】:2016-04-03 02:49:10
【问题描述】:

Pragmatic Programmers' RSpec Book 中的练习是编写一个名为 Codebreakers 的问题解决游戏。目的是在 Ruby 中使用 Cucumber 和 Rspec 接触 BDD。

以下是我的 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

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

感谢您的建议和见解!

谢谢。

【问题讨论】:

    标签: ruby rspec cucumber bdd


    【解决方案1】:

    我相信您需要将您的 spec_helper.rb 中的要求更改为这样

    require 'codebreaker/codebreaker'
    

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

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

    【讨论】:

    • 哇,我以为我已经尝试过了,但显然没有。谢谢迈尔斯斯坦菲尔德。那行得通。
    • 很高兴听到@kellychex。接受这个答案?请点赞?
    • OP:如果此答案解决了您的问题/问题,您应该接受它。这将有助于保持整洁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2014-05-27
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多