[AWDwR4] Iteration E 中遇到的问题汇总

Q1. teration E1中,p142页 执行rake test:units 后出错:

1) Failure:
test_product_price_must_be_positive(ProductTest) [/home/csd/railsProjects/depot/test/unit/product_test.rb:36]:
Failed assertion, no message given.

看代码好像没什么错误的地方,hailong提醒我可以用 rails console --sandbox 命令, 使用 --sandbox 参数时, 对数据库的所有修改都会在退出console时回滚。我按照测试用例中的数据new了一个 Product p,然后查看p的valid属性,果然为false,再去查看 p.errors信息,发现是我照着书上的代码写例子时,嫌 title字段太长了,随手输的title太短了导致的。在rails console中输入的命令如下:

1.9.3p194 :001 > p = Product.new(:title => "Awd sds",
1.9.3p194 :002 >     :description => "yyy",
1.9.3p194 :003 >     :image_url => "fred.jpg",
1.9.3p194 :004 >     :price => 1 )
1.9.3p194 :005 > p.valid?
 Product Exists (0.2ms)  SELECT 1 AS one FROM "products" WHERE "products"."title" = 'Awd sds' LIMIT 1
 => false
1.9.3p194 :006 > p.errors
 => #<ActiveModel::Errors:0x0000000326ecf8 @base=#<Product id: nil, title: "Awd sds", description: "yyy", image_url: "fred.jpg", price: #<BigDecimal:3371808,'0.1E1',9(36)>, created_at: nil, updated_at: nil>, @messages={:title=>["It's too short"]}>

PS:

  跑测试时,如果想在测试过程中打印一些信息,可以使用 puts 来输出信息,rails 3 中,使用 rake 时加上 --trace 参数或者使用
ruby -Itest test/unit/product_test.rb 也可以,参考链接

回到本系列的目录

posted on 2012-10-31 22:16  生栋  阅读(254)  评论(0编辑  收藏  举报

导航