The Last Day Of Summer

.NET技术 C# ASP.net ActiveReport SICP 代码生成 报表应用 RDLC
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以前的随笔都是按照书上的例子写下来的,但是,这次在测试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_bookNull的错误:

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 !@product.save

    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