Forum: Ruby on Rails Agile book - test error

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.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2005-12-23 17:04
(Received via mailing list)
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.
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 Peter Donald (Guest)
on 2005-12-24 00:01
(Received via mailing list)
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 Donald

Blog: http://www.RealityForge.org
D323e0daf5c222c68f89e0566002d0cc?d=identicon&s=25 Douglas Tan (Guest)
on 2005-12-24 03:41
(Received via mailing list)
another way to access the fixtures using the default settings in
test_helper
would be to use:

version_control_book = products(:version_control_book)
588b32371b62ebe36d70fa712a1173f0?d=identicon&s=25 Gerard J. Petersen (Guest)
on 2005-12-24 09:59
(Received via mailing list)
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 Donald tried to type something
like:
> --
> >
> > end
> > Ran the command: "ruby test/unit/product_test.rb"
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2005-12-24 10:05
(Received via mailing list)
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 Tan tried to type something
like:
> > test/test_helper.rb and add something like
> > Blog: 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
> > > Rails@lists.rubyonrails.org
> > > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > 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!
Bfc3ae865ab5cb781b9d1d4b50ab132b?d=identicon&s=25 David Rupp (Guest)
on 2005-12-24 20:00
(Received via mailing list)
>
> 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
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2005-12-26 12:06
(Received via mailing list)
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 Rupp 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!
This topic is locked and can not be replied to.