Skip to content

Instantly share code, notes, and snippets.

@jurezove
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jurezove/57d65c3140535b33d72b to your computer and use it in GitHub Desktop.
Save jurezove/57d65c3140535b33d72b to your computer and use it in GitHub Desktop.
Object Oriented relationship using Test Driven approach.
# Engine stuff
class Engine;end
class MotorcycleEngine < Engine
def start
"Wroom!"
end
end
class CarEngine < Engine
def start
"Wrooooooooooom!"
end
end
module EnginePowered
def start_engine
@engine.start
end
def travel
"#{start_engine} #{super}"
end
end
# Vehicles
class Vehicle
def travel
"Traveling in Warp speed with a #{self.class}."
end
end
class Bike < Vehicle
end
class Car < Vehicle
include EnginePowered
def initialize
@engine = CarEngine.new
end
end
class Motorcycle < Vehicle
include EnginePowered
def initialize
@engine = MotorcycleEngine.new
end
end
require 'minitest/autorun'
require './main.rb'
describe Vehicle do
let(:vehicle) { Vehicle.new }
it 'should be able to travel' do
vehicle.travel.must_equal 'Traveling in Warp speed with a Vehicle.'
end
end
describe Bike do
let(:bike) { Bike.new }
it 'shouldn\'t have an engine' do
refute_respond_to(bike, :start_engine)
end
it 'shouldn\'t start an engine before' do
bike.travel.must_equal 'Traveling in Warp speed with a Bike.'
end
end
describe Car do
let(:car) { Car.new }
it 'should be able start the engine' do
car.start_engine.must_equal 'Wrooooooooooom!'
end
it 'should start the engine before traveling' do
car.travel.must_equal 'Wrooooooooooom! Traveling in Warp speed with a Car.'
end
end
describe Motorcycle do
let(:motorcycle) { Motorcycle.new }
it 'should be able start the engine' do
motorcycle.start_engine.must_equal 'Wroom!'
end
it 'should start the engine before traveling' do
motorcycle.travel.must_equal 'Wroom! Traveling in Warp speed with a Motorcycle.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment