I am a Ruby beginner currently working on learning how to work with RSpec. I am also working on a temperature converter and I have gotten it to pass the RSpec tests for my ftoc
(Fahrenheit to Celsius) but I am having problems trying to pass the last test of my ctof
function. When "body temperature" is passed through my ctof
method, it is expected to return a value be_within(0.1).of(98.6)
and instead I have been only able to make it return 98 and the test doesn't pass. How does the be_within work? how can I get the desired value (be_within(0.1).of(98.6)
) without affecting my other tests?
Here my code:
def ftoc(fahrenheit_degrees)
celsius = (fahrenheit_degrees.to_i - 32) * 5.0 / 9.0
celsius.round
end
def ctof(celsius_degrees)
fahrenheit = (celsius_degrees.to_i * 9 / 5) + 32
fahrenheit.round
end
Here my RSpec code :
describe "temperature conversion functions" do
describe "#ftoc" do
it "converts freezing temperature" do
expect(ftoc(32)).to eq(0)
end
it "converts boiling temperature" do
expect(ftoc(212)).to eq(100)
end
it "converts body temperature" do
expect(ftoc(98.6)).to eq(37)
end
it "converts arbitrary temperature" do
expect(ftoc(68)).to eq(20)
end
end
describe "#ctof" do
it "converts freezing temperature" do
expect(ctof(0)).to eq(32)
end
it "converts boiling temperature" do
expect(ctof(100)).to eq(212)
end
it "converts arbitrary temperature" do
expect(ctof(20)).to eq(68)
end
it "converts body temperature" do
expect(ctof(37)).to be_within(0.1).of(98.6) #Here my problem :/
end
end
end