Topic: Unit Test does not Generate Instance Variables from YML

I am coding my first unit tests. I expected instance variables to be created for each instance named record in my test fixture, but it doesn't seem to be happening. (I'm running Ruby 1.8.2, RoR 1.1.6 should that be relevant.

This is what's in publications.yml:

pub_single_ed:
    id: 1
    name: Single-Edition

pub_multi_ed:
    id: 2
    name: Multi-Edition


And this is one of my unit tests:
require File.dirname(__FILE__) + '/../test_helper'

class PublicationTest < Test::Unit::TestCase
  fixtures :publications

  def setup
    @pub = Publication.find(1)
  end

  # Replace this with your real tests.
  def test_create
    assert_kind_of Publication, @pub
    assert_equal publications("pub_single_ed").name, @pub.name
    assert_equal @pub_single_ed.name, @pub.name  # <<<< This line causes an error
  end
end


What I thought would happen is that I could either use the syntax publications("pub_single_ed").name or @pub_single_ed.name to refer to the values in the fixture definition. The line with the instance variable syntax generates an error.

NoMethodError: You have a nil object when you didn't expect it!
The error occured while evaluating nil.name
    test/unit/publication_test.rb:14:in `test_create'

Was this feature eliminated in some version somewhere along the way, or do I need to do something special to enable it. Thanks.

Re: Unit Test does not Generate Instance Variables from YML

bobf wrote:

Was this feature eliminated in some version somewhere along the way, or do I need to do something special to enable it. Thanks.

Yep, it was removed due to performance reasons. IIRC you can enable it, but I wouldn't recommend it.

Railscasts - Free Ruby on Rails Screencasts

Re: Unit Test does not Generate Instance Variables from YML

As ryanb already pointed out the load all fixtures into instance variables was removed.  The near "canonical" way to reference a fixture from a test is using the function with a symbol argument, (though as you've seen the string works as well).

So the normal way you'd need to see this is:

    assert_equal publications(:pub_single_ed).name, @pub.name

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.