0

注意:我是 Ruby 和编程新手。我有一个名为的类,JourneyLog我正在尝试获取一个方法start来实例化另一个类的新实例,称为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正确地注入课程,但这就是我所能得到的。有人可以提供一些帮助吗?

4

1 回答 1

1

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

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

在规范文件中,您需要传递journey_classJourneyLog构造函数:

describe JourneyLog do
  subject { described_class.new(journey_class: journey_class) }
  # ...
于 2019-02-02T17:19:06.213 回答