Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created October 9, 2013 12:44
Show Gist options
  • Save davidbella/6900679 to your computer and use it in GitHub Desktop.
Save davidbella/6900679 to your computer and use it in GitHub Desktop.
Ruby: Creating a test spec with simplecov for a jukebox
require 'simplecov'
SimpleCov.start
require 'json'
require 'rspec'
require_relative '../lib/jukebox'
require_relative '../lib/song'
#use this song data for your tests
songs = [
"The Phoenix - 1901",
"Tokyo Police Club - Wait Up",
"Sufjan Stevens - Too Much",
"The Naked and the Famous - Young Blood",
"(Far From) Home - Tiga",
"The Cults - Abducted",
"The Phoenix - Consolation Prizes"
]
describe Song do
it 'should have a name' do
song = Song.new('1901')
end
end
describe Jukebox do
let(:jukebox) {Jukebox.new(songs.map {|song| Song.new(song)})}
it 'should be on and have a list of songs' do
jukebox.on?
jukebox.songs.should be_a_kind_of(Array)
end
it 'should return a help string when calling for help' do
jukebox.help.should eq("Please select help, list, exit, or play.")
end
it 'should say goodbye and be off when calling exit' do
jukebox.exit.should eq("Goodbye, thanks for listening!")
!jukebox.on?
end
it 'should present a list of songs when calling list' do
song_list = jukebox.list
song_list.should be_a_kind_of(String)
song_list.should include('1901')
end
it 'should return a now playing string when calling play' do
jukebox.play('1901').should eq("now playing 1901")
end
describe '#command' do
it 'should receive a message for help, list, or exit' do
['help', 'list', 'exit'].each do |cmd|
jukebox.send(:command, cmd)
jukebox.should respond_to(cmd.to_sym)
end
end
it 'should return a message when asking to play a song' do
jukebox.command('play 1901').should eq('now playing 1901')
end
it 'should provide an invalid response for unknown command' do
jukebox.command('hjybjhl').should eq('invalid command')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment