Skip to content

Instantly share code, notes, and snippets.

@yhordi
Last active August 29, 2015 14:26
Show Gist options
  • Save yhordi/0ebc10dfe8c844228169 to your computer and use it in GitHub Desktop.
Save yhordi/0ebc10dfe8c844228169 to your computer and use it in GitHub Desktop.
Code from my testing lecture
#this is the enterprise class with driver code at the bottom
class Enterprise
attr_accessor :crew, :loadout, :assignment
def initialize
@crew = []
@loadout = { beam: nil, projectile: nil }
@assignment = { on_mission: [], on_ship: [], on_leave: [] }
end
def beam_up(crewmember)
@crew << crewmember
end
def beam_down(crewmember)
@crew.delete_if do |staff|
staff == crewmember
end
end
def assign(crewmember, post)
@assignment[post] << crewmember
end
def arm_beam(weapon)
@loadout[:beam] = weapon
end
def arm_projectile(weapon)
@loadout[:projectile] = weapon
end
end
enterprise_d = Enterprise.new
enterprise_d.beam_up("Captain Jean Luc Picard")
enterprise_d.beam_up("Commander William T. Riker")
enterprise_d.beam_up("Lieutenant Commander Natasha Yar")
p enterprise_d.crew[0] == "Captain Jean Luc Picard"
enterprise_d.beam_down("Lieutenant Commander Natasha Yar")
p enterprise_d.crew[2] == nil
enterprise_d.assign("Commander William T. Riker", :on_leave)
p enterprise_d.assignment[:on_leave][0] == "Commander William T. Riker"
enterprise_d.arm_beam("Phaser")
p enterprise_d.loadout[:beam] == "Phaser"
enterprise_d.arm_projectile("Photon Torpedoes")
p enterprise_d.loadout[:projectile] == "Photon Torpedoes"
require_relative 'enterprise'
describe Enterprise do
let(:enterprise_d) { Enterprise.new }
before(:each) do
enterprise_d.beam_up('Picard')
end
describe '#beam_up' do
it 'adds a member to the crew array' do
expect(enterprise_d.crew).to(include('Picard'))
end
end
describe '#beam_down' do
it 'removes a member from the crew array' do
enterprise_d.beam_up('Stupid Riker')
enterprise_d.beam_down('Picard')
expect(enterprise_d.crew).to_not include('Picard')
end
end
end
class ShuttleCraft
def initialize
@hatch = false
end
def open_hatch
@hatch = true
'The hatch is now open'
end
def hatch_open?
@hatch
end
end
require_relative 'shuttle_craft'
describe ShuttleCraft do
let(:shuttle) { ShuttleCraft.new }
describe '#open_hatch' do
it 'opens the hatch.' do
expect(shuttle.open_hatch).to eq('The hatch is now open')
end
it 'returns false if the hatch is closed' do
expect(shuttle.hatch_open?).to eq(false)
end
it 'returns true if hatch is open' do
shuttle.open_hatch
expect(shuttle.hatch_open?).to eq(true)
end
end
end
@yhordi
Copy link
Author

yhordi commented Jul 28, 2015

https://www.relishapp.com/ is a great resource for rspec syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment