Forum: Ruby on Rails fixtures instance variable naming

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.
66c6fb05619a0835909eacbe3acffe55?d=identicon&s=25 Matthew Collins (Guest)
on 2006-06-08 16:37
(Received via mailing list)
hi all,

trying my first foray into testing and having trouble with accessing
the fixture instance variable

built a test for my NewsRelease model.

have a news_releases.yml as a fixture with:

first:
   id: 1
   title: my first release
   body: this is still my first test of a news release
   publish: 0
   date_publication: 2006-06-06 17:28:00


in news_release_test.rb, i have

def setup
     @news_release = NewsRelease.find(1)
end

def test_create
     assert_kind_of NewsRelease, @news_release
     assert_equal @news_release["first"]["id"], @news_release.id
end

and i receive:

"NoMethodError: 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.[]"


and i first tried (from the agile book)

def test_create
     assert_kind_of NewsRelease, @news_release
     assert_equal @first.id, @news_release.id
end


and i get:
test_create(NewsReleaseTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if
you really wanted the id of nil, use object_id

so, finally -- what is the correct syntax for accessing the fixtures
in my tests?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-08 20:18
Matthew Collins wrote:
> so, finally -- what is the correct syntax for accessing the fixtures
> in my tests?

first make sure you are including the fixture in the test with the
"fixtures :foobars" line.  Then use "foobars(:name_of_item_in_yaml)" to
access them.

  class NewsReleaseTest < Test::Unit::TestCase
    fixtures :news_releases

    def setup
      @news_release = NewsRelease.find(1)
    end

    def test_create
      assert_kind_of NewsRelease, @news_release
      assert_equal news_releases(:first).id, @news_release.id
    end
  end
This topic is locked and can not be replied to.