注意:我是 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
正确地注入课程,但这就是我所能得到的。有人可以提供一些帮助吗?