Skip to content

Instantly share code, notes, and snippets.

@scottcreynolds
Last active May 30, 2018 22:21
Show Gist options
  • Save scottcreynolds/6889882 to your computer and use it in GitHub Desktop.
Save scottcreynolds/6889882 to your computer and use it in GitHub Desktop.

##RSpec Code Coverage Lab

How do we know we have enough tests, and that our tests cover all of our code?

Enter simplecov! simplecov is a tool that will measure your test run against the code paths in your code files and see if you're exercising them all. "Code Paths" includes method calls, conditional statements, loops, and anything that branches program flow.

This is important because you want to be able to know that every decision your program makes is being tested, no matter what.

Assignment

Given the above Jukebox and Song classes, write the tests required in jukebox_spec.rb to get yourself to 100% code coverage on simplecov.

Instructions

  1. Install the simplecov gem.
  2. Run your spec file with rspec

You should see output at the end of the test results that looks something like: Coverage report generated for RSpec to /Users/scottreynolds/code/ta/labs/simplecov/coverage. 14 / 33 LOC (42.42%) covered.

  1. Open coverage/index.html in your browser window to view the report.
  2. When you hit 100% test coverage, submit either a gist or a repository of your solution and the coverage report
# A Jukebox filled with songs
# that responds to help, list, play, and exit
require_relative 'song'
class Jukebox
attr_accessor :songs
APPROVED_COMMANDS = [:list, :help, :exit]
def initialize(songs)
@songs = songs
@on = true
end
def on?
@on
end
def help
"Please select help, list, exit, or play."
end
def command(input)
response = ''
if APPROVED_COMMANDS.include?(input.strip.downcase.to_sym)
response = self.send(input)
elsif input.start_with?("play")
song_request = input.split("play").last.strip
response = self.play(song_request)
else
response = "invalid command"
end
response
end
def exit
@on = false
"Goodbye, thanks for listening!"
end
def list
list = ""
songs.each_with_index do |song, i|
list += "#{i+1} #{song.name}\n"
end
list
end
def play(song_request)
"now playing #{song_request}"
end
end
require 'simplecov'
SimpleCov.start
require 'json'
require 'rspec'
require_relative 'jukebox'
require_relative '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
end
describe Jukebox do
end
class Song
attr_accessor :name
def initialize(name)
@name = name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment