views:

229

answers:

1

I have an extremely simple unit test. I'm not sure why it fails when the expected is the same as the actual!

Here is the method I am testing:

def calibration_constant
  big_decimal = self.coefficient * (10**self.exponent)
  return big_decimal.to_f
end

Note, that coefficient is "6.1" and exponent is "1"

Here is the relevant test line:

assert_equal(61.0.to_f, calibrations[0].calibration_constant)

Here is the output:

test_calibration_constants(PyranometerCalibrationTest): <61.0> expected but was <61.0>.

I can't for the life of me figure out why it would claim to fail, when clearly they are equal. Does anyone have any input on this situation? All of my other similar test cases work fine.

I am using Rails 2.1 (Ruby 1.8) on Windows with SQL Server 2005.

Thanks in advance for your help.

+10  A: 

You're comparing two floats. Try using assert_in_delta with a really low delta value (0.000001) to see if your problem is just a precision error.

Bill the Lizard
That worked. I would think that Rails would display the entire number on the output when it fails to alert the developer that that might be the case.Thanks for your response.
JP
You would think that *at least* in an assert error message that would be the case. I can't say why it doesn't behave that way.
Bill the Lizard