Forum: Ruby on Rails Strange Unit Testing error - newbie question

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.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-09 10:41
I'm fairly new to Rails, and I've been learning from AGILE WEB
DEVELOPMENT WITH RAILS.  Everything has been going smoothly until the
chapter on Unit Testing, where I'm getting some strange errors.

I'm up to page 141 in the Agile book, and attempting to run the test
program containing the method test_read_with_hash.  The previous tests
work properly, but I don't seem to be able to refer to the fixtures
through its variable.

The file "product_test.rb" contains the line "fixtures :products", which
says it should set up the variable @fixtures through which I can refer
to my fixtures.  My "products.yml" fixture file is exactly as downloaded
from the Agile Web Dev site, I've checked it for syntax and spelling and
tabs-instead-of-spaces and the like.

"product_test.rb", in the method "test_read_with_hash" contains this
line:

    vc_book = @products["version_control_book"]

When I run product_test.rb, I get a NoMethodError on this line:

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:71:in `test_read_with_hash'

My actual "depot" application works perfectly as far as I can tell, and
I'd like to debug this but I'm not sure how to trace it back.  What
could cause this error, how would I go about finding it?

Thanks in advance,
Jeff Coleman
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-10 03:50
Just checking again to see if anyone has any ideas on this. What could
lead the unit testing processes to be unable to form an array of
fixtures?

Jeff Coleman wrote:
>
> I'm fairly new to Rails, and I've been learning from AGILE WEB
> DEVELOPMENT WITH RAILS.  Everything has been going smoothly until the
> chapter on Unit Testing, where I'm getting some strange errors.
>
> I'm up to page 141 in the Agile book, and attempting to run the test
> program containing the method test_read_with_hash.  The previous tests
> work properly, but I don't seem to be able to refer to the fixtures
> through its variable.
>
> The file "product_test.rb" contains the line "fixtures :products", which
> says it should set up the variable @fixtures through which I can refer
> to my fixtures.  My "products.yml" fixture file is exactly as downloaded
> from the Agile Web Dev site, I've checked it for syntax and spelling and
> tabs-instead-of-spaces and the like.
>
> "product_test.rb", in the method "test_read_with_hash" contains this
> line:
>
>     vc_book = @products["version_control_book"]
>
> When I run product_test.rb, I get a NoMethodError on this line:
>
> 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:71:in `test_read_with_hash'
>
> My actual "depot" application works perfectly as far as I can tell, and
> I'd like to debug this but I'm not sure how to trace it back.  What
> could cause this error, how would I go about finding it?
>
> Thanks in advance,
> Jeff Coleman
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-03-10 06:42
(Received via mailing list)
A few things have changed regarding unit testing since the first edition
of the book.  Read this... http://clarkware.com/cgi/blosxom/2005/10/24

_Kevin
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-03-10 07:10
Jeff Coleman wrote:
>
>
>     vc_book = @products["version_control_book"]
>
> When I run product_test.rb, I get a NoMethodError on this line:

You're probably running rails 1.0 and the book was pre-1.0; and
unfortunately the way that fixtures are handled changed in 1.0.

@products is not automatically created for you like the book says.
However, an easy method is defined for you instead, called products():

vc_book = products(:version_control_book)

This tripped me up when I upgraded to 1.0 back in December and
unfortunately continues to be a problem for anyone who now buys the
book.

Other than that, the book is awesome, and not too many other things are
out of date.  You should check the errata site for the book while you're
at it (I don't have the url at hand, go to www.pragmaticprogrammer.com
and I'm sure you can find it).

Jeff
www.softiesonrails.com
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-10 11:22
Thanks!  That's definitely something I didn't know.

Jeff Coleman
This topic is locked and can not be replied to.