Forum: Ruby on Rails Agile Web Dev unit test fails with fixture instance variable

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.
Lindsay B. (Guest)
on 2006-05-22 15:53
I'm following Agile Edition 1. On page 148 it suggests using instance
variables named after the fixture, for example:

assert_equal @version_control_book.id, @product.id

When I introduce this type of instance variable into my test, I get the
following error:

  1) Error:
test_not_owner(GroupTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you
really wanted the id of nil, use object_id
    test/unit/group_test.rb:8:in `setup_without_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:548:in
`setup'

Line 8:

@group = Group.find(@alice_group.id)

where alice_group is my Group fixture.

I know there are a few issues with the testing section of the Agile book
- have I just hit one?

thanks
Lindsay.
unknown (Guest)
on 2006-05-22 16:52
(Received via mailing list)
On May 22, 2006, at 6:53 AM, Lindsay B. wrote:

> test_not_owner(GroupTest):
>
> where alice_group is my Group fixture.
>
> I know there are a few issues with the testing section of the Agile
> book
> - have I just hit one?

Yes, this is an artifact of the book coming out while Rails was still
in flux. The right way to reference a fixture is to use alternative
syntax like so: fixture_name(:fixture_element)

Using the depot application as the example, the book uses the old
syntax (like on page 148). Instead of @version_control_book.id the
syntax should be products(:version_control_book).id.

In your specific example, the code should probably be groups
(:alice_group).id (assuming you have a line "fixtures :groups" in
your test).

Hope this helped.

cr
This topic is locked and can not be replied to.