Topic: Unexpected nil object errors from test fixture?

Hi. Just started with Ruby, coming from a J2EE, .NET, and PHP background.

I'm having some trouble with a unit test. It appears that including a YML fixture in my test case causes the following error:

Exception: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.-
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:112:in `unlock_mutex'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:534:in `teardown'
C:/ruby/lib/ruby/1.8/test/unit/testcase.rb:79:in `run'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
C:/ruby/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
C:/ruby/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in `run_suite'
C:/Program Files/eclipse/plugins/org.rubypeople.rdt.testunit_0.8.1.609062100PRD/ruby/RemoteTestRunner.rb:107:in `start_mediator'
C:/Program Files/eclipse/plugins/org.rubypeople.rdt.testunit_0.8.1.609062100PRD/ruby/RemoteTestRunner.rb:52:in `start'
C:/Program Files/eclipse/plugins/org.rubypeople.rdt.testunit_0.8.1.609062100PRD/ruby/RemoteTestRunner.rb:272

The unit test code is:
require File.dirname(__FILE__) + '/../test_helper'

class UserTest < Test::Unit::TestCase
  fixtures :users
 
  #test empty invalid
  def test_empty_attributes
    usr = User.new
    assert !usr.valid?
    assert usr.errors.invalid?(:first_name)
    assert usr.errors.invalid?(:last_name)
    assert usr.errors.invalid?(:login_name)
    assert usr.errors.invalid?(:pin)
    assert usr.errors.invalid?(:password)
    assert usr.errors.invalid?(:email)
  end
 
  #test not empty valid
  def test_filled_attributes
    usr = User.new
    usr.first_name = "vorname"
    usr.last_name = "nachname"
    usr.login_name = "vor_nach"
    usr.pin = "54646*#"
    usr.password = "h4x0r3d!"
    usr.email = "l33t-ly@tripractix.com"
    assert usr.valid?
  end
 
  #test email addresss format
  def test_invalid_email
    assert true
  end
 
  #test uniqueness of email per organization
  #test uniqueness of login name per organization
  #test validity for password complexity
  #test validity for pin complexity
  #test updated > created
  #test destroy user ~ destroy usergroups_users
  #test destryo user ~ destroy events_users
  #test destroy user ~ destroy documents_users
end


The model
class User < ActiveRecord::Base
  has_and_belongs_to_many :usergroups#, :dependent => :destroy
  has_and_belongs_to_many :events#, :dependent => :destroy
  has_and_belongs_to_many :documents
 
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :login_name
  validates_presence_of :pin
  validates_presence_of :password
  validates_presence_of :email
end

Lastly the YML fixture
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
james:
    id: 1
bob:
    id: 2
    first_name: firstname
    last_name: lastname
    login_name: first_last
    pin: 654
    password: secret
    email: l33tly@tripractix.com
    created_on: 2007-01-01 13:36:00
    updated_on: 2007-01-15 13:36:00

When I comment out "fixtures :users" the tests pass. I was hoping to be able to use test fixtures as my user model has some more complex relationships and will need to be setup in many places to properly test the rest of the model.

What am I missing? What is going wrong?

If it matters I am running the tests from Eclipse with the Ruby Dev Tools plugin against MySQL 5.

TIA.

Re: Unexpected nil object errors from test fixture?

What happens when you remove the james record from the users.yml file? Sometimes some code elsewhere expects certain attributes in the table/model to be filled. I don't know if this is the case since the stack trace doesn't seem to be calling much else, but worth a try.

Railscasts - Free Ruby on Rails Screencasts

Re: Unexpected nil object errors from test fixture?

Found it!

It seems that using TAB to indent causes problems. When I change my TABs to spaces it works.

Any thoughts if this is a bug in Ruby or a bug in RDT or a bug in Eclipse?

Re: Unexpected nil object errors from test fixture?

Good find. It's actually a restriction of YAML files. For some reason they don't allow tabs, only spaces. I have no clue why though, I imagine that would be simple to fix.

Railscasts - Free Ruby on Rails Screencasts