Created
August 12, 2018 20:46
-
-
Save lukemorton/ce7b1e0fb85f56385c116cfee6487508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example One: spec/unit/lib/space/flight/ship_gateway_spec.rb | |
RSpec.describe Space::Flight::ShipGateway do | |
let(:ship) { FactoryBot.create(:ship) } | |
let(:gateway) { described_class.new(ship_repository: Ship) } | |
context 'when finding a ship by id' do | |
subject { gateway.find_by_id(ship.id) } | |
it 'returns ship with correct id' do | |
expect(subject.id).to eq(ship.id) | |
end | |
it 'returns ship with correct name' do | |
expect(subject.name).to eq(ship.name) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example Two: spec/unit/lib/space/flight/travel_spec.rb | |
RSpec.describe Space::Flight::Travel do | |
let(:ship) { FactoryBot.create(:ship) } | |
let(:new_location) { FactoryBot.create(:location) } | |
let(:ship_gateway) { Ship } | |
let(:use_case) { described_class.new(ship_gateway: ship_gateway) } | |
context 'when travelling to new location' do | |
subject { use_case.travel(ship.id, to: new_location) } | |
it 'updates ships location' do | |
expect{subject}.to change{ship.location}.to(new_location) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example Three: spec/unit/lib/space/flight/ship_gateway_spec.rb | |
RSpec.describe Space::Flight::ShipGateway do | |
let(:ship) { FactoryBot.create(:ship) } | |
let(:gateway) { described_class.new(ship_repository: Ship) } | |
context 'when finding a ship by id' do | |
subject { gateway.find_by_id(ship.id) } | |
it 'returns ship' do | |
expect(subject).to eq(ship) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/unit/lib/space/flight/ship_gateway_spec.rb | |
RSpec.describe Space::Flight::ShipGateway do | |
let(:ship) { instance_double('Ship', id: 1) } | |
let(:ship_class) { class_double('Ship', find_by: ship) } | |
let(:gateway) { described_class.new(ship_repository: ship_class) } | |
context 'when finding a ship by id' do | |
subject { gateway.find_by_id(ship.id) } | |
it 'returns ship with correct id' do | |
expect(subject.id).to eq(ship.id) | |
end | |
it 'returns ship with correct name' do | |
expect(subject.name).to eq(ship.name) | |
end | |
it 'retrieves ship from model' do | |
subject | |
expect(ship_class).to have_received(:find_by) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/unit/lib/space/flight/travel_spec.rb | |
RSpec.describe Space::Flight::Travel do | |
let(:ship_id) { 1 } | |
let(:new_location_id) { 1 } | |
let(:ship_gateway) { instance_double('Space::Flight::ShipGateway', update: true) } | |
let(:use_case) { described_class.new(ship_gateway: ship_gateway) } | |
context 'when travelling to new location' do | |
subject { use_case.travel(ship_id, to: new_location_id) } | |
it 'updates ships location' do | |
subject | |
expect(ship_gateway).to have_received(:update).with(ship_id, location_id: new_location_id) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/space/flight/ship.rb | |
module Space | |
module Flight | |
Ship = Struct.new(:id, :name) | |
end | |
end | |
# lib/space/flight/ship_gateway.rb | |
require_relative 'ship' | |
module Space | |
module Flight | |
class ShipGateway | |
def initialize(ship_repository:) | |
@ship_repository = ship_repository | |
end | |
def find(ship_id) | |
ship_record = ship_repository.find_by(id: ship_id) | |
build_ship(ship_record) unless ship_record.nil? | |
end | |
private | |
def build_ship(ship_record) | |
Ship.new( | |
ship_record.id, | |
ship_record.name | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment