# Unit testing confusion

**URL:** <https://www.ruby-forum.com/t/unit-testing-confusion/55324>\
**Category:** Ruby\
**Created:** [March 19, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/unit-testing-confusion/55324 "2006-03-19T19:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [March 19, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/unit-testing-confusion/55324/1 "2006-03-19T19:28:33Z")

</div>

Hey all,

I’m following along with AWDWR, specifically the unit testing.  
On page 148, it shows you how Test::Unit::TestCase will automatically  
define instance variables based on your fixtures.

It doesn’t seem to actually work, though. It seems that both @artists  
[“beatles”][“name”] and @beatles.name are nil objects in my test,  
where as artists(:beatles).name works ok.

What is the right way of doing it? I’d guess the one that actually  
works is right, but there’s a lot of stuff on google about the other  
ways that I can’t get to work.

The actual error messages for the failed assertions are different:

@artists[“beatles”][“name”]:

test\_create(ArtistTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name  
test/unit/artist\_test.rb:15:in `test\_create’

@beatles.name:

test\_create(ArtistTest):  
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.[]  
test/unit/artist\_test.rb:14:in `test\_create’

test/fixtures/artists.yml:

beatles:  
id: 1  
name: The Beatles

test/unit/artist\_test.rb:

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class ArtistTest \< Test::Unit::TestCase  
fixtures :artists

```
 def setup
     @artist = Artist.find(1)
 end

 def test_create
     assert_equal @artists["beatles"]["name"], @artist.name #

```

fails  
assert\_equal @beatles.name, @artist.name #  
fails  
assert\_equal artists(:beatles).name, @artist.name #  
works  
end  
end

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [March 19, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/unit-testing-confusion/55324/2 "2006-03-19T19:31:36Z")

</div>

Sorry, wrong mailing list \>\_\<

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [March 20, 2006, 9:48am UTC](https://www.ruby-forum.com/t/unit-testing-confusion/55324/3 "2006-03-20T09:48:12Z")

</div>

you need to set “instantiated\_fixtures” to true in test\_helper.rb:

self.use\_instantiated\_fixtures = true

kind regards
