Ruby on rails开发从头来(windows)(二十一)-测试Model时的问题2011-12-02 博客园 Cure以前的随笔都是按照书上的例子写下来的,但是,这次在测试Model时,按照书上的例子代码怎么也走不通,所以就换个方式,这篇变成了提问。按照书上的说法,在products_test.rb开始的时候,会根据定义的yml文件,加载测试数据到一个Hash里,这样,在test的时候我们就可以根据yml文件中用例的名字来访问一个product对象,例如:
def test_read_with_hashassert_kind_of Product, @productvc_book = @products["version_control_book"]assert_equal vc_book["id"], @product.idassert_equal vc_book["title"], @product.titleassert_equal vc_book["description"], @product.descriptionassert_equal vc_book["image_url"], @product.image_urlassert_equal vc_book["price"], @product.priceassert_equal vc_book["date_available"], @product.date_available_before_type_castend
但是,根据在运行测试的时候总是提示vc_book为Null的错误:3) 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 occurred while evaluating nil.[] test/unit/product_test.rb:41:in `test_read_with_hash"
怎么回事呢?即使将product_test.rb里的内容全部替换成书中完整的代码也还是不行,是不是版本问题?