Forum: Ruby on Rails Accessing fixtures from unit tests

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.
Paul B. (Guest)
on 2006-04-28 19:25
(Received via mailing list)
I have set up some fixtures in test/fixtures/users.xml:

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
admin:
  id: 1
  username: admin
  password: admin
pbarry:
  id: 2
  username: pbarry

I have a unit test for my user model:

require File.dirname(__FILE__) + '/../test_helper'

class UserTest < Test::Unit::TestCase
  fixtures :users

  def test_find
    user = User.find(@admin.id)
    assert_equals(@admin.id,user.id)
  end

end


When I run the test, I get this:

C:\ruby\workspace\lms>ruby test/unit/user_test.rb
Loaded suite test/unit/user_test
Started
E
Finished in 0.156 seconds.

  1) Error:
test_find(UserTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you
really wanted the id of nil, use object_id
    test/unit/user_test.rb:7:in `test_find'

1 tests, 0 assertions, 0 failures, 1 errors

Why is @admin nil?  Shouldn't it be populated from the fixture?
Jean-François (Guest)
on 2006-04-28 19:41
(Received via mailing list)
Hello Paul,

2006/4/28, Paul B. <removed_email_address@domain.invalid>:
[...]
>   def test_find
>     user = User.find(@admin.id)
>     assert_equals(@admin.id,user.id)
>   end
>
> end

[...]
>  Why is @admin nil?  Shouldn't it be populated from the fixture?

Because the instantiated fixtures are disabled by default.
You shoud use sth like :

  admin = users(:admin)
  user = User.find(admin.id)
  assert_equals admin.id, user.id

     -- Jean-François.
Paul B. (Guest)
on 2006-04-28 20:09
(Received via mailing list)
Thanks, that did it.  This must be one of those new to Rails 1.1 things?
We
desparately need a new version of Agile Web Dev with Rails updated for
1.1.
I know the plan was to not "break the book", but I think there a number
of
these little details that have unfortunately done that.
This topic is locked and can not be replied to.