依赖注入导致 Rspec 失败和 IRB 失败

Dependency Injection causing both Rspec failure and IRB failure

注意:我是 Ruby 和编程新手。 我有一个名为 JourneyLog 的 class 我正在尝试获取一个名为 start 的方法来实例化另一个 class 的新实例,名为 Journey

class JourneyLog
  attr_reader :journey_class

   def initialize(journey_class: Journey)
    @journey_class = journey_class
    @journeys = []
  end

  def start(station)
   journey_class.new(entry_station: station)
 end
end

当我进入 irb 我遇到以下问题

    2.2.3 :001 > require './lib/journeylog'
     => true
    2.2.3 :002 > journeylog = JourneyLog.new
    NameError: uninitialized constant JourneyLog::Journey
    from /Users/BartJudge/Desktop/Makers_2018/oystercard-challenge/lib/journeylog.rb:4:in `initialize'
    from (irb):2:in `new'
    from (irb):2
    from /Users/BartJudge/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
2.2.3 :003 >

我还有以下Rspec测试

require 'journeylog'
describe JourneyLog do
  let(:journey) { double :journey, entry_station: nil, complete?: false, fare: 1}
  let(:station) { double :station }
  let(:journey_class) { double :journey_class, new: journey }

  describe '#start' do
    it 'starts a journey' do
      expect(journey_class).to receive(:new).with(entry_station: station)
      subject.start(station)
    end

  end
end

我得到以下 Rspec 失败;

1) JourneyLog#start starts a journey
     Failure/Error: expect(journey_class).to receive(:new).with(entry_station: station)

       (Double :journey_class).new({:entry_station=>#<Double :station>})
           expected: 1 time with arguments: ({:entry_station=>#<Double :station>})
           received: 0 times
     # ./spec/jorneylog_spec.rb:9:in `block (3 levels) in <top (required)>'

我完全不知道问题是什么,或者在哪里寻找一些答案。 我假设我没有正确地注入 Journey class,但这就是我能做到的。 有人可以提供一些帮助吗?

journeylog.rb 文件中您需要加载 Journey class:

require 'journey' # I guess the Journey class is defined in lib/journey.rb

在规格文件中,您需要将 journey_class 传递给 JourneyLog 构造函数:

describe JourneyLog do
  subject { described_class.new(journey_class: journey_class) }
  # ...