Agile Web Development with Rails MODEL测试框架 test_* (ProductTest) 出现很多错误

可能很多朋友在参照第一版 <Pragmatic Bookshelf,Agile Web Development with Rails >写测试的时候碰到了大量的错误,这些不是因为我们的代码抄错了,而是由于我们不是很了解这个测试框架的一些配置参数。
我的环境
Ubuntu  7.10
Eclipse Version: 
3.3.0  + RadRails
Mysql 
5 .x
Ruby 
1.8.6
Rails 
1.2.4

这是我的代码
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_truth
    assert_kind_of Product
,   @product
  end
  
  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
    a 
=   @product . price . to_s
    assert_equal 
" 29.95 "   ,  a
    assert_equal 
" 2005-01-26 00:00:00 " ,   @product . date_available_before_type_cast
  end
  
  def test_update
    assert_equal 
" 29.95 " ,   @product . price . to_s
    
@product . price  =   99.99
    assert 
@product . save ,   @product . errors . full_messages . join ( " " )
    
@product . reload
    assert_equal 
" 99.99 "   , @product . price . to_s
  end
  
  def test_destroy
    
@product . destroy
    assert_raise(ActiveRecord
:: RecordNotFound){ Product . find( @product . id) }
  end
  
  def test_validate
    assert_equal 
" 29.95 " ,   @product . price . to_s
    
@product . price  =   0.00
    assert 
! @product . save
    assert_equal 
1 ,   @product . errors . count
    assert_equal 
" should be positive " ,   @product . errors . on( : price)
  end
  
  def test_read_with_hash
    assert_kind_of Product
,   @product
    vc_book 
=   @products [ " version_control_book " ]
    assert_equal vc_book[
" id " ] ,   @product . id
    assert_equal vc_book[
" title " ] ,   @product . title
    assert_equal vc_book[
" description " ] ,   @product . description
    assert_equal vc_book[
" image_url " ] ,   @product . image_url
    assert_equal vc_book[
" price " ] ,   @product . price
    assert_equal vc_book[
" date_available " ] ,   @product . date_available_before_type_cast
  end
  
  def test_read_with_fixture_variable
    assert_kind_of Product
,   @product
    assert_equal 
@version_control_book . id ,   @product . id
    assert_equal 
@version_control_book . title ,   @product . title
    assert_equal 
@version_control_book . description ,   @product . description
    assert_equal 
@version_control_book . image_url ,   @product . image_url
    assert_equal 
@version_control_book . price ,   @product . price
    assert_equal 
@version_control_book . date_available ,   @product . date_available
  end
  
  
end

如果直接运行可能会出现一些错误:
NoMethodError with test_read_with_hash

Failure:
test_create(ProductTest) 
[ test/unit/product_test.rb:16 ] :
<
29 > expected but was
<#<BigDecimal:4aad7b0
, ' 0.2995E2 ' , 8 ( 8 )>>.

Error:
test_update(ProductTest):
ActiveRecord::RecordNotFound: Couldn't find Product with ID
= 1

需要我们修改参数了
depot/test/test_helper.rb
set
self.use_transactional_fixtures  =  false #原本希望通过事务回滚来还原数据
self.use_instantiated_fixtures  
=  true

详细解释:
      在使用Mysql的时候Talbe的默认storage engine 是MyISAM,而Rails的测试框架是默认是希望通过事务回滚机制来还原数据。但是MyISAM是不支持回滚的,所以无法还原数据,导致假象上的生命周期讲解有问题。所以也可以通过switching from MyISAM to InnoDB 来达到同样的效果。
      这样就可以了。不过不能怪写书的,毕竟在用例子作为讲义时难免由于直接讲配置不容易理解而导致错误的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值