Forum: Ruby on Rails NoMethodError with test_read_with_hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
74514db32c88a02b6d7743f4d441e330?d=identicon&s=25 Mike Agres (seriousnoob)
on 2006-04-21 04:32
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?
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-04-21 08:49
Mike Agres 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
74514db32c88a02b6d7743f4d441e330?d=identicon&s=25 Mike Agres (seriousnoob)
on 2006-04-22 00:56
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
This topic is locked and can not be replied to.