Skip to content

Instantly share code, notes, and snippets.

@vike27
Created April 28, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vike27/2ae0d425f46c01851e15 to your computer and use it in GitHub Desktop.
Save vike27/2ae0d425f46c01851e15 to your computer and use it in GitHub Desktop.
FizzBuzz Minitest Exercise
# I wanted learn about Minitest and I saw a cool youtube video about creating a FizzBuzz class that required Minitest. It seemed like a # great introduction so I decided to try it for myself and do my own implementation here
#1st mkdir fizzbuzz to create folder/directory
#This is the main class which we are testing, put this code in a 'fizzbuzz.rb' ruby file
class FizzBuzz
def print(num)
if num.respond_to?(:each)
final_arr = []
num.each do |x|
final_arr << test(x)
end
final_arr
else
test(num)
end
end
private
def test(num)
raise ArgumentError unless num > 0
raise ArgumentError, "Integers only" unless num.is_a? Integer
if num % 15 == 0
return "FizzBuzz"
elsif num % 5 == 0
return "Buzz"
elsif num % 3 == 0
return "Fizz"
else
return num.to_s
end
end
end
# below is the content of the spec file with minitest, put this is a ruby file like 'spec.rb'
require 'minitest/autorun'
require_relative 'fizzbuzz'
describe "FizzBuzz" do
before do
@fb = FizzBuzz.new
@number = 15*rand(1..100) + 1
end
it "return Fizz for multiples of 3" do
@fb.print(3*@number).must_equal "Fizz"
end
it "returns Buzz for multiples of 5" do
@fb.print(5*@number).must_equal "Buzz"
end
it "returns FizzBuzz for multiples of 15" do
@fb.print(15*@number).must_equal "FizzBuzz"
end
it "returns the number as a string for all non FizzBuzz numbers" do
@fb.print(@number).must_equal @number.to_s
end
it "does not return FizzBuzz for a non multiple of 15" do
@fb.print(@number).wont_equal "FizzBuzz"
end
it "returns an array when we give an array object as an argument" do
@fb.print(1..5).must_be_instance_of Array
end
it "raises an error if the number is negative" do
proc {@fb.print(-1)}.must_raise ArgumentError
end
it "raises an error if the argument is 0" do
proc {@fb.print(0)}.must_raise ArgumentError
end
it "raises an error if the number is not an integer" do
proc {@fb.print(3.5)}.must_raise ArgumentError
end
end
#To run the test suite just go to the file directory which in my case was a fizzbuzz folder(mkdir fizzbuzz) in your command line and run 'ruby spec.rb' file without the quotes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment