Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created February 7, 2012 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jimweirich/1758146 to your computer and use it in GitHub Desktop.
Save jimweirich/1758146 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 3
class InvalidScoreError < StandardError
end
class Presentation
attr_reader :title, :presenter
def initialize(title, presenter)
@title = title
@presenter = presenter
@total = 0.0
@count = 0
end
def add_score(n)
fail InvalidScoreError if n > 5 || n < 1
@total += n
@count += 1
end
def average_score
@total / @count
end
end
require 'spec_helper'
require 'presentation'
describe Presentation do
let(:presentation) { Presentation.new("TITLE", "PRESENTER") }
it "has a title and presenter" do
presentation.title.should == "TITLE"
presentation.presenter.should == "PRESENTER"
end
it "can have scores added to it" do
lambda { presentation.add_score(4) }.should_not raise_error
end
it "can calculate the average score with a single score" do
presentation.add_score(4)
presentation.average_score.should == 4
end
it "can calculate the average of two scores" do
presentation.add_score(4)
presentation.add_score(5)
presentation.average_score.should be_within(0.001).of(4.5)
end
it "rejects too large scores with an error" do
lambda {
presentation.add_score(6)
}.should raise_error(InvalidScoreError)
end
it "rejects too small scores with an error" do
lambda {
presentation.add_score(0)
}.should raise_error(InvalidScoreError)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment