Created
June 4, 2013 03:09
-
-
Save jcarley/5703307 to your computer and use it in GitHub Desktop.
Refactoring ruby brownbag
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
class FizzBuzz | |
def self.evaluate(number) | |
raise "Not a number" unless number.is_a? Integer | |
return "fizzbuzz" if number % 5 == 0 && number % 3 == 0 | |
return "fizz" if number % 3 == 0 | |
return "buzz" if number % 5 == 0 | |
number.to_s | |
end | |
end | |
describe FizzBuzz do | |
describe "#evaluate" do | |
it 'evaluates a number' do | |
expect { described_class.evaluate(1) }.to_not raise_error "Not a number" | |
end | |
it 'raises an error when not given an Integer' do | |
expect { described_class.evaluate("not a number") }.to raise_error "Not a number" | |
end | |
context "when given a number not divisible by 3 or 5" do | |
it { expect(described_class.evaluate(7)).to eql "7" } | |
end | |
context "when given a number divisible by 3" do | |
it { expect(described_class.evaluate(3)).to eql "fizz" } | |
it { expect(described_class.evaluate(9)).to eql "fizz" } | |
end | |
context "when given a number divisible by 5" do | |
it { expect(described_class.evaluate(5)).to eql "buzz" } | |
it { expect(described_class.evaluate(10)).to eql "buzz" } | |
end | |
context "when given a number divisible by 3 and 5" do | |
it { expect(described_class.evaluate(15)).to eql "fizzbuzz" } | |
it { expect(described_class.evaluate(30)).to eql "fizzbuzz" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment