Agile book - test error


#1

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.


#2

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


#3

another way to access the fixtures using the default settings in
test_helper
would be to use:

version_control_book = products(:version_control_book)


#4

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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!


#5

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:inmethod_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:inrequire’
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

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


Rails mailing list
removed_email_address@domain.invalid
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!


#6

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!


#7

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:intest_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:inrequire’
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