RSpec.describe "Fizz Buzz Tests" do
it "should return `Fizz` for number divisible by 3" do
expect(fizzBuzz(3)).to eq "Fizz"
end
it "should return `Buzz` for number divisible by 5" do
expect(fizzBuzz(5)).to eq "Buzz"
end
it "should return `FizzBuzz` for number divisible by 3 and 5" do
expect(fizzBuzz(15)).to eq "FizzBuzz"
end
it "should return `FizzBuzz` for number divisible by 3 and 5" do
expect(fizzBuzz(90)).to eq "FizzBuzz"
end
it "should return 7 since it is indivisible by 3 and 5" do
expect(fizzBuzz(7)).to eq 7
end
end
这是我的代码:
def fizz_buzz (number)
if number%3==0&&number%5==0
puts ("FizzBuzz")
elsif number%3==0
puts ("Fizz")
elsif number%5==0
puts ("Buzz")
else
puts (number)
end
end
fizz_buzz(3)
fizz_buzz(5)
fizz_buzz(15)
fizz_buzz(90)
fizz_buzz(7)
请帮忙。
答案 0 :(得分:0)
fizzBuzz
方法应该返回值,而不是打印它们。
require 'rspec'
def fizzBuzz(number)
if number % 3 == 0 && number % 5 == 0
"FizzBuzz"
elsif number % 3 == 0
"Fizz"
elsif number % 5 == 0
"Buzz"
else
number
end
end
RSpec.describe "Fizz Buzz Tests" do
it "should return `Fizz` for number divisible by 3" do
expect(fizzBuzz(3)).to eq "Fizz"
end
it "should return `Buzz` for number divisible by 5" do
expect(fizzBuzz(5)).to eq "Buzz"
end
it "should return `FizzBuzz` for number divisible by 3 and 5" do
expect(fizzBuzz(15)).to eq "FizzBuzz"
end
it "should return `FizzBuzz` for number divisible by 3 and 5" do
expect(fizzBuzz(90)).to eq "FizzBuzz"
end
it "should return 7 since it is indivisible by 3 and 5" do
expect(fizzBuzz(7)).to eq 7
end
end