论坛首页 Ruby版 rails

如何在测试文件中定义数据断言

浏览 1122 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-06-04
在定义断言的时候,如果需要测试的是数据,就会出错,正确来说是失败.
我把29.95改成29,错误信息还是一样,但是书本上就是写的这样的.我确信没有写错.
但是为什么总是不让通过呢?
运行后控制台给出的信息:
Loaded suite D:/rubyproject/depot/test/unit/product_test
Started
F
Finished in 0.515 seconds.

  1) Failure:
test_create(ProductTest) [D:/rubyproject/depot/test/unit/product_test.rb:17]:
<29.95> expected but was
<#<BigDecimal:47f624c,'0.2995E2',8(8)>>.

1 tests, 6 assertions, 1 failures, 0 errors


测试程序:
require File.dirname(__FILE__) + '/../test_helper'

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

  def setup
    @product = Product.find(1)
  end
  
  # Replace this with your real tests.
  def test_create
    assert_kind_of Product, @product
    assert_equal 1, @product.id
    assert_equal "Pragmatic Version Control", @product.title
    assert_equal "How to use version control", @product.description
    assert_equal "http://www.rubyonrails.org/images/awdr2.gif", @product.image_url
    assert_equal 29.95, @product.price
    assert_equal "2007-05-30 20:07:28", @product.date_available_before_type_cast
  end
end


夹具文件: products.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
version_control_book:
 id: 1
 title: Pragmatic Version Control
 description: How to use version control
 image_url: http://www.rubyonrails.org/images/awdr2.gif
 price: 29.95
 date_available: 2007-05-30 20:07:28
automation_book:
 id: 2
 title: Pragmatic Project Automation
 description: How to automate your project
 image_url:  http://images.dangdang.com/images/9240772_b.jpg
 price: 39.95
 date_available: 2007-05-31 20:07:28
   
最后更新时间:2007-06-04
数据库栏位类型有误
   
0 请登录后投票
最后更新时间:2007-06-04
Readonly 写道
数据库栏位类型有误


可是我数据库的定义是含两位小数的数据类型呀!
+----------------+---------------+------+-----+---------+----------------+
| Field          | Type          | Null | Key | Default | Extra          |
+----------------+---------------+------+-----+---------+----------------+
| id             | int(11)       | NO   | PRI | NULL    | auto_increment |
| title          | varchar(100)  | NO   |     |         |                |
| description    | text          | NO   |     |         |                |
| image_url      | varchar(200)  | NO   |     |         |                |
| price          | decimal(10,2) | NO   |     |         |                |
| date_available | datetime      | NO   |     |         |                |
+----------------+---------------+------+-----+---------+----------------+


下面是我建表的SQL语句,我看不出来有什么问题。
drop table if exists products;
create table products (
  id			int		not null auto_increment,
  title			varchar(100)	not null,
  description		text		not null,
  image_url		varchar(200)	not null,
  price			decimal(10,2)	not null,
  date_available	datetime	not null,
  primary key (id)
);
   
0 请登录后投票
最后更新时间:2007-06-04
数据库用float或者double类型
   
0 请登录后投票
最后更新时间:2007-06-07
Readonly 写道
数据库用float或者double类型



但是在数据库中定义为decimal(10,2)类型,RUBY类对应的类型就是float类型的呀!
难道有问题么?
   
0 请登录后投票
最后更新时间:2007-06-07
huihua 写道
Readonly 写道
数据库用float或者double类型

但是在数据库中定义为decimal(10,2)类型,RUBY类对应的类型就是float类型的呀!
难道有问题么?
哪里说的?如果是mysql,rails是会帮你转成BigDecimal的,书上写的清清楚楚。

而且既然是float类型,应该用assert_in_delta方法
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐