一个关于Test Fixtures的问题
《应用Rails进行敏捷Web开发》第12章 任务T:测试
代码:
?
- require?File.dirname(__FILE__)?+?'/../test_helper'??
- ??
- class?ProductTest?<?Test::Unit::TestCase??
- ??fixtures?:products??
- ??
- ??def?setup??
- ????@product?=?Product.find(1)??
- ??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??
- end??
测试失败,提示信息:
Loaded suite test/unit/product_test
Started
E
Finished in 0.235 seconds.
? 1) Error:
test_read_with_hash(ProductTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
??? test/unit/product_test.rb:12:in `test_read_with_hash'
1 tests, 1 assertions, 0 failures, 1 errors
请问这是咋回事?谢谢。
Rails版本1.1.6。Test Fixtures 文件 products.yml
- version_control_book:??
- ??id:??????????????????????? 1??
- ??title:???????????????????? Pragmatic?Version?Control??
- ??description:???????????????How?to?use?version?control??
- ??image_url:?????????????????http:/images/Pragmatic?Version?Control.jpg??
- ??price:?????????????????????29.95??
- ??date_available:??????????? 2006-05-10?00:00:00??
- automation_book:??
- ??id:?????????????????????? 2??
- ??title:??????????????????? Pragmatic?Project?Automation??
- ??description:??????????????How?to?automate?your?project??
- ??image_url:????????????????http:/images/Pragmatic?Project?Automation.jpg??
- ??price:????????????????????29.95??
- ??date_available:?????????? 2006-05-10?00:00:00??
将test_helper.rb中的self.use_instantiated_fixtures设置成true就可以了。