程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Ruby on rails開發從頭來(windows)(二十一)-測試Model時的問題

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

編輯:關於JAVA

以前的隨筆都是按照書上的例子寫下來的,但是,這次在測試Model時,按照書上的例子代碼怎麼也走不通,所以就換個方式,這篇變成了提問。

按照書上的說法,在products_test.rb開始的時候,會根據定義的yml文件,加載測試數據到一個Hash裡,這樣,在test的時候我們就可以根據yml文件中用例的名字來訪問一個product對象,例如:

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

但是,根據在運行測試的時候總是提示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裡的內容全部替換成書中完整的代碼也還是不行,是不是版本問題?

附product_test.rb全部代碼:

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
  assert_equal 29.95, @product.price
  assert_equal "2005-01-26 00:00:00",
  @product.date_available_before_type_cast
 end
 def test_update
  assert_equal 29.95, @product.price
  @product.price = 99.99
  assert @product.save, @product.errors.full_messages.join("; ")
  @product.reload
  assert_equal 99.99, @product.price
 end
 #~ def test_destroy
  #~ @product.destroy
  #~ assert_raise(ActiveRecord::RecordNotFound) { Product.find(@product.id) }
 #~ end
 def test_validate
  assert_equal 29.95, @product.price
  @product.price = 0.00
  assert [email protected]
  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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved