Welcome 微信登录

首页 / 软件开发 / JAVA / Ruby on rails开发从头来(windows)(二十)-测试model(CRUD)

Ruby on rails开发从头来(windows)(二十)-测试model(CRUD)2011-12-02 博客园 Cure上篇随笔简单了解了rails的测试和测试用数据的使用,这次来看看怎样对一个model进行添删查改的测试。

1.还是使用上次写的products_test.rb,修改test_turth方法的名字为test_create,并且使其内容为:

 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://.../sk_svn_small.jpg", @product.image_url  assert_equal 29.95,@product.price  assert_equal "2005-01-26 00:00:00",  @product.date_available_before_type_cast end
然后运行测试命令:depot>ruby test/unit/product_test.rb,屏幕上会显示信息:

Loaded suite test/unit/product_testStartedFFinished in 0.109 seconds.1) Failure:test_create(ProductTest) [test/unit/product_test.rb:16]:<29> expected but was<#<BigDecimal:4aad7b0,"0.2995E2",8(8)>>.1 tests, 6 assertions, 1 failures, 0 errors
我们看到,是assert_equal 29.95,@product.price断言失败了。根据《Agile Web Development with Rails》里的内容,这句断言应该是正常通过的。但是不知道是不是版本或环境的问题,我自己写的时候总是不行。为了能够使断言通过,我们修改一下,把

assert_equal 29.95,@product.price

改为:assert_equal "29.95",@product.price_before_type_cast

我们看到了,product对象的每个属性都有对应的_before_type_cast版本,其内容是一个字符串。

现在再次运行测试命令,得到的结果如下:

Loaded suite test/unit/product_testStarted.Finished in 0.078 seconds.
1 tests, 7 assertions, 0 failures, 0 errors

从上面的测试中看到,我们在setup方法中,从数据库中查找了id为1的记录,然后在test_create方法中对其的属性逐个判断测试。