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
Hey David, David Turnbull wrote:> 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.You have to set self.use_instantiated_fixtures = true in test_helper.rb for this to work. Michael
On Mon, 2006-03-20 at 05:32 +1100, David Turnbull wrote:> 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---- I believe that Dave has suggested that his section on testing is somewhat broken and it would probably be a very good idea to read through the errata page before wasting too much time on it. http://books.pragprog.com/titles/rails/errata Craig