Forum: Ruby on Rails Test Unit Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fd73105128b3ea537e42a5dabc490960?d=identicon&s=25 Bruce (Guest)
on 2005-12-31 04:44
Hello All,

I am experiencing the following test unit error working with Rails that
I can't fathom.

I have a products.yml file which holds the following test fixture for
the products table:

dell_pc:
 id: 1
 title: Dell PC
 description: Dell PC
 image_url: http://.../pc_image.jpg
 price: 15000.00
 date_available: 2005-12-31 06:53:00

I also have the following product_test.rb file to test the fixture
above:

require File.dirname(__FILE__) + '/../test_helper'

class ProductTest < Test::Unit::TestCase
  fixtures :products

  def setup
    @product = Product.find(1)
  end

  def test_fixtures
    assert_kind_of Product, @product
		assert_equal 1, @product.id
		assert_equal "Dell PC", @product.title
		assert_equal "Dell PC", @product.description
		assert_equal "http://.../pc_image.jpg", @product.image_url
		puts "class of @product.price = #{@product.price.class}"
		puts "value of @product.price = #{@product.price}"
		assert_equal 15000.00, @product.price
		assert_equal "2005-12-31 06:53:00",
@product.date_available_before_type_cast
  end

end

When I run ruby test/unit/product_test.rb I receive the following
results:

Loaded suite test/unit/product_test
Started
class of @product.price = Float
value of @product.price = 15000.0
F
Finished in 5.485 seconds.

  1) Failure:
test_fixtures(ProductTest) [test/unit/product_test.rb:18]:
<15000.0> expected but was
<15000.0>.

1 tests, 6 assertions, 1 failures, 0 errors

Does anyone have any suggestions on how to resolve this issue?

Many thanks,
Bruce.
Da2dbf48d2f400ccff8f0df127798451?d=identicon&s=25 Derek Haynes (Guest)
on 2005-12-31 09:38
(Received via mailing list)
So 15000.0 is a float in both values?

- Derek

On 12/30/05, Bruce <phicorp@iinet.net.au> wrote:
>  title: Dell PC
> class ProductTest < Test::Unit::TestCase
>                 assert_equal "Dell PC", @product.description
> When I run ruby test/unit/product_test.rb I receive the following
> test_fixtures(ProductTest) [test/unit/product_test.rb:18]:
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Derek Haynes
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2005-12-31 19:20
(Received via mailing list)
Bruce wrote:

>  title: Dell PC
> class ProductTest < Test::Unit::TestCase
> 		assert_equal "Dell PC", @product.description
> When I run ruby test/unit/product_test.rb I receive the following
> test_fixtures(ProductTest) [test/unit/product_test.rb:18]:
> <15000.0> expected but was
> <15000.0>.
>
> 1 tests, 6 assertions, 1 failures, 0 errors
>
> Does anyone have any suggestions on how to resolve this issue?

Because Float is not an exact representation, it is not a good idea to
expect precise equality of Floats. Try using assert_in_delta instead of
assert_equals:

assert_in_delta(expected_float, actual_float, delta, message="")

Passes if expected_float and actual_float are equal within delta
tolerance.

http://www.ruby-doc.org/stdlib/libdoc/test/unit/rd...

regards

   Justin
This topic is locked and can not be replied to.