Forum: Ruby on Rails Accessing fixtures from unit tests

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and 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
  id: 1
  username: admin
  password: admin
  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(


When I run the test, I get this:

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

  1) Error:
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(
>     assert_equals(,
>   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(

     -- 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?
desparately need a new version of Agile Web Dev with Rails updated for
I know the plan was to not "break the book", but I think there a number
these little details that have unfortunately done that.
This topic is locked and can not be replied to.