NoMethodError with test_read_with_hash


#1

While running the product_test.rb test fixture in the Agile Web
Development book (p. 148), but when I run

ruby test/unit/product_test.rb, I get the following error:

  1. 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 occured while evaluating nil.[]
    test/unit/product_test.rb:39:in `test_read_with_hash’

4 tests, 15 assertions, 0 failures, 1 errors

Below is my code for the the method:

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.price
assert_equal vc_book[“price”], @product.price
assert_equal vc_book[“date_available”],
@product.date_available_before_type_cast
end

Is there anything I need to tweak in the code to avoid seeing the error
again?


#2

Mike A. wrote:

While running the product_test.rb test fixture in the Agile Web
Development book (p. 148), but when I run

ruby test/unit/product_test.rb, I get the following error:

  1. 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 occured while evaluating nil.[]
    test/unit/product_test.rb:39:in `test_read_with_hash’

4 tests, 15 assertions, 0 failures, 1 errors

Below is my code for the the method:

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.price
assert_equal vc_book[“price”], @product.price
assert_equal vc_book[“date_available”],
@product.date_available_before_type_cast
end

Is there anything I need to tweak in the code to avoid seeing the error
again?

Hi,

I guess auto instantiated fixtures are now turned off by default, so
this would be products(:version_control_book); if you prefer to use the
auto instantiated fixtures turn them on in test helper.


Agnieszka


#3

Cool - I set self.use_instantiated_fixtures to “true” in test_helper.rb
and that allowed me to run the test without a hitch.

Thanks!

-Mike