Agile Web Development with Rails MODEL测试框架 test_* (ProductTest) 出现很多错误
2008-04-06 15:43
609 查看
可能很多朋友在参照第一版<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 来达到同样的效果。
这样就可以了。不过不能怪写书的,毕竟在用例子作为讲义时难免由于直接讲配置不容易理解而导致错误的。
我的环境
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 来达到同样的效果。
这样就可以了。不过不能怪写书的,毕竟在用例子作为讲义时难免由于直接讲配置不容易理解而导致错误的。
相关文章推荐
- Agile Web Development with Rails 读书笔记 (二)
- Agile Web Development with Rails第十章笔记——任务E:更智能的购物车
- Agile Web Development with Rails 翻译(五)
- Agile Web Development with Rails 翻译(十二 )
- agile WEB development with rails的笔记
- 开始读《Agile Web Development with Rails》
- 《Agile Web Development With Rails》读后感--rails基于web设计的best Practices
- Agile Web Development with Rails 翻译(十三)
- Agile Web Development With Rails下载
- Agile Web Development with Rails 读书笔记 (四)
- Agile Web Development with Rails 翻译(一)
- Agile Web Development with Rails 翻译(六)
- 《Agile Web Development with Rails》读书笔记(二)
- 《Agile Web Development with Rails》读书笔记(四)
- Agile Web Development with Rails
- Agile Web Development with Rails 翻译(二)
- Agile Web Development with Rails 翻译(七)
- Agile Web Development with Rails 翻译(十四)
- Agile Web Development with Rails第六章笔记——任务A:创建应用程序
- Agile Web Development with Rails 读书笔记 (三)