Named fixtures and automatic instance variables; newbie prob


#1

According to Agile Web D. with Rails (pg 148), a fixture like

version_control_book:
id: 1
title: Pragmatic Version Control

loaded as “fixtures: products”

can be accessed as @products[“version_control_book”] or
@version_control_book within a unit test (TestCase).

My (very simple) tests are working otherwise, but the equivalents of
@products and @version_control_book are nil. Why?

patients.yml is:

newborn:
id: 1
hosp_ident: 120001010
last_name: Musa
other_names: BabyA
sex: F
… etc.

patients_test.rb is


require File.dirname(FILE) + ‘/…/test_helper’
class PatientTest < Test::Unit::TestCase
fixtures :patients

def setup
@patient = Patient.find(1)
end

def test_truth
puts “@patients = #{@patients.inspect}”
puts “@newborn = #{@newborn}”
assert_kind_of Patient, @patient
assert_equal 1, @patient.id
assert_equal ‘F’, @patient.sex
assert_equal ‘-’, @patient.rv
… etc
end
end

and the output is:

[mike@eeyore ptbase]$ ruby test/unit/patient_test.rb
Loaded suite test/unit/patient_test
Started
nil
.
Finished in 0.166667 seconds.

1 tests, 4 assertions, 0 failures, 0 errors
[mike@eeyore ptbase]$ ruby test/unit/patient_test.rb
Loaded suite test/unit/patient_test
Started
@patients = nil
@newborn =
.
Finished in 0.134237 seconds.

1 tests, 4 assertions, 0 failures, 0 errors


#2

Mike B. wrote:

According to Agile Web D. with Rails (pg 148), a fixture like

version_control_book:
id: 1
title: Pragmatic Version Control

loaded as “fixtures: products”

can be accessed as @products[“version_control_book”] or
@version_control_book within a unit test (TestCase).

You should post to the Rails list, not this Ruby list.

But…

The book is out of date if you’re running rails 1.0. Instantiated
fixtures are no longer enabled by default.

You need to do:

mybook = products(:version_control_book)

etc.


#3

Thanks, Jeff.