以前的隨筆都是按照書上的例子寫下來的,但是,這次在測試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裡的內容全部替換成書中完整的代碼也還是不行,是不是版本問題?