Ruby on rails開發從頭來(windows)(二十一)-測試Model時的問題

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.