Fixtures instance variable naming


#1

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?


#2

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