Strange Unit Testing error - newbie question

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 C.man

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 C.man 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 C.man

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

Jeff C.man 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

Thanks! That’s definitely something I didn’t know.

Jeff C.man