Skip to content

Instantly share code, notes, and snippets.

@leoallen85
Last active October 22, 2015 14:11
Show Gist options
  • Save leoallen85/771259e77e5853c994ac to your computer and use it in GitHub Desktop.
Save leoallen85/771259e77e5853c994ac to your computer and use it in GitHub Desktop.
Dependency Injection in the constructor
describe JourneyLog do
subject(:journey_log) { described_class.new(journey_klass: journey_klass) }
let(:journey_klass) { double(:journey_klass) }
let(:journey) { double(:journey) }
describe "Starting a journey" do
it "adds a journey to the log" do
allow(journey_klass).to receive(:new).with(:station).and_return(journey)
journey_log.start_journey(:station)
expect(journey_log.journeys).to include(journey)
end
end
end
class JourneyLog
attr_reader :journeys
def initialize(journey_klass: Journey)
@journeys = []
@journey_klass = journey_klass
end
def start_journey(station)
journeys << journey_klass.new(station)
end
private
attr_reader :journey_klass
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment