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.
--
Posted via http://www.ruby-forum.com/.
So 15000.0 is a float in both values? - Derek On 12/30/05, Bruce <phicorp@iinet.net.au> wrote:> 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. > > -- > 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
Bruce wrote:> 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?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/rdoc/classes/Test/Unit/Assertions.html#M004536 regards Justin