# Agile book - test error

**URL:** <https://www.ruby-forum.com/t/agile-book-test-error/74475>\
**Category:** Rails\
**Created:** [December 23, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475 "2005-12-23T17:04:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 23, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/1 "2005-12-23T17:04:58Z")

</div>

Hi all,

I get an error when testing the “store\_controller\_test.rb”

The troublesome line is in the test below:

def test\_add\_to\_cart  
get :add\_to\_cart, :id =\> @version\_control\_book.id \<\<\<  
cart = session[:cart]  
assert\_equal @version\_control\_book.price, cart.total\_price  
assert\_redirected\_to :action =\> ‘display\_cart’  
follow\_redirect  
assert\_equal 1, assigns(:items).size  
assert\_template “store/display\_cart”  
end

And the error n question is:

1. Error:  
test\_add\_to\_cart(StoreControllerTest):  
RuntimeError: Called id for nil, which would mistakenly be 4 – if you  
really  
wanted the id of nil, use object\_id  
test/functional/store\_controller\_test.rb:26:in `test\_add\_to\_cart’

Ran the command: “ruby test/unit/product\_test.rb”

Checked the database table and the fixture. The are loaded. Rests to see  
I’m  
clueless.

Thanx for any brain timecycles spent on this.

Regards,

Gerard.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 24, 2005, 12:01am UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/2 "2005-12-24T00:01:28Z")

</div>

Hi,

The defaults for tests changed in latest released version of Rails to  
decrease running times of tests. What you need to do is go into  
test/test\_helper.rb and add something like  
“self.use\_instantiated\_fixtures = true” and the objects should be  
loaded.

Hope that helps.

–  
Cheers,

Peter D.

Blog: [http://www.RealityForge.org](http://www.RealityForge.org)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 24, 2005, 3:41am UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/3 "2005-12-24T03:41:00Z")

</div>

another way to access the fixtures using the default settings in  
test\_helper  
would be to use:

version\_control\_book = products(:version\_control\_book)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 24, 2005, 9:59am UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/4 "2005-12-24T09:59:16Z")

</div>

Peter,

# This one was already set (to ‘false’ though), changed it but made no difference. Does the other one in there (self.use\_transactional\_fixtures

true) relate as a logical OR, so either transactional or instantiated?

Regards,

Gerard.

On Saturday 24 December 2005 00:01, Peter D. tried to type something  
like:

> –
> 
> > end  
> > Ran the command: “ruby test/unit/product\_test.rb”
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
$biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
$fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
~  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 24, 2005, 10:05am UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/5 "2005-12-24T10:05:17Z")

</div>

Douglas,

What exactly does this mean? Am I filling a variable with a hash  
reference?

btw tried it and it came back with the following

When put in the definition “test\_add\_to\_cart” itself

1. Error:  
test\_add\_to\_cart(StoreControllerTest):  
NoMethodError: undefined method `products' for #<StoreControllerTest:0x408746dc> /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/test_process.rb:377:in `method\_missing’  
test/functional/store\_controller\_test.rb:26:in `test\_add\_to\_cart’

And when put in test\_helper.rb  
./test/functional/…/test\_helper.rb:29: undefined method `products' for Test::Unit::TestCase:Class (NoMethodError) from test/functional/store_controller_test.rb:1:in `require’  
from test/functional/store\_controller\_test.rb:1

Regards,

Gerard.

On Saturday 24 December 2005 03:38, Douglas T. tried to type something  
like:

> > test/test\_helper.rb and add something like  
> > Blog: [http://www.RealityForge.org](http://www.RealityForge.org)
> > 
> > > ```
> > > cart = session[:cart]
> > > 
> > > ```
> > > 
> > > test\_add\_to\_cart(StoreControllerTest):  
> > > Checked the database table and the fixture. The are loaded. Rests to  
> > > Gerard.
> > > 
> > > * * *
> > > 
> > > Rails mailing list  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 26, 2005, 12:06pm UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/6 "2005-12-26T12:06:02Z")

</div>

David,

Thanx a lot for the very clear explanation. This helps me understand the  
test  
scenario’s from the book a lot better.

Regards,

Gerard.

On Saturday 24 December 2005 19:57, David R. tried to type something  
like:

> which is a symbol identifying the name of the specific fixture you
> 
> > And when put in test\_helper.rb  
> > sure you’re using the proper variable style – products  
> > (:fixture\_name) – and you should be okay.
> 
> Hope this helps,  
> David

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 24, 2005, 8:00pm UTC](https://www.ruby-forum.com/t/agile-book-test-error/74475/7 "2005-12-24T20:00:34Z")

</div>

> What exactly does this mean? Am I filling a variable with a hash  
> reference?

Not exactly. When you declare your fixtures, Rails creates one of two  
things:

1. an instance variable for each fixture in the fixtures file (when  
self.use\_instantiated\_fixtures == true; this yields variables like  
@version\_control\_book, etc.)
2. an accessor method named after the entire file (products, in this  
case), when self.use\_transactional\_fixtures == false

In the first case, which is the style used in the book, each  
individual fixture, which equates to a row in the database, becomes  
an instance variable that you can access using @fixture\_name.

In the second case, which is the default style in Rails 1.0, all  
fixtures in a file are loaded once and Rails creates a method in your  
subclass of Test::Unit::TestCase that allows you to access each of  
the fixtures defined in the file by name. So, because you have your  
fixtures in a file called ‘products.yml’, Rails creates an accessor  
method called products() which takes a single (required) parameter,  
which is a symbol identifying the name of the specific fixture you  
want – in this case products(:version\_control\_book).

For best results, you want the two class variables that define the  
different behaviors to be mutually exclusive. When  
use\_transactional\_fixtures == true, you want  
use\_instantiated\_fixtures == false, and vice versa. There’s a chance  
that a given style will work with both set to true, but it’s not  
worth the possible confusion.

> `method_missing' test/functional/store_controller_test.rb:26:in`test\_add\_to\_cart’
> 
> And when put in test\_helper.rb  
> ./test/functional/…/test\_helper.rb:29: undefined method `products' for Test::Unit::TestCase:Class (NoMethodError) from test/functional/store_controller_test.rb:1:in`require’  
> from test/functional/store\_controller\_test.rb:1

This is a signal that Rails wasn’t able (or didn’t try) to create the  
accessor method I described above, which I’m guessing means it didn’t  
pick up the signal to use the transactional style. Make sure the  
transactional one is true and the instantiated one is false, and make  
sure you’re using the proper variable style – products  
(:fixture\_name) – and you should be okay.

Hope this helps,  
David
