Forum: Ruby Named fixtures and automatic instance variables; newbie prob

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.
Mike B. (Guest)
on 2006-03-17 19:23
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
Jeff C. (Guest)
on 2006-03-17 19:28
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.
Mike B. (Guest)
on 2006-03-18 09:31
Thanks, Jeff.
This topic is locked and can not be replied to.