Topic: Using fixture names

I've been trying to use dynamic fixture names to make my tests a but less brittle, but it's just not working.

I have a fixture called 'galleries' with a couple of entries in it; 'gallery_with_update' and 'not_visible_gallery'..

According to the agile web dev book, this should work..

require File.dirname(__FILE__) + '/../test_helper'

class GalleryTest < Test::Unit::TestCase
  fixtures :galleries
 
  def setup
    @gallery = Gallery.find(1)
  end
 
  def test_create_with_gallery_with_update
    assert_kind_of Gallery, @gallery
    assert_equal @gallery_with_update.id, @gallery.id
  end
 
end


It works if I change @gallery_with_update.id back to 1, but like this, when I run the test, I get this error:

Loaded suite test/unit/gallery_test
Started
E
Finished in 0.080633 seconds.

  1) Error:
test_create_with_gallery_with_update(GalleryTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
    test/unit/gallery_test.rb:12:in `test_create_with_gallery_with_update'


What am I doing wrong? I've tried all sorts of variations to see if it would work, but no luck. According to the book, @galleries["gallery_with_update"] should also do the same thing, but it doesnt work either..

thanks for any help..

Last edited by idlefingers (2006-07-11 11:02:18)

Re: Using fixture names

IIRC, this feature was removed after the Agile Development with Rails book was written because it slowed down the tests too much. I believe you can turn it back on, but I don't recommend it. Instead, do this:

require File.dirname(__FILE__) + '/../test_helper'

class GalleryTest < Test::Unit::TestCase
  fixtures :galleries
 
  def setup
    @gallery = Gallery.find(1)
  end
 
  def test_create_with_gallery_with_update
    assert_kind_of Gallery, @gallery
    assert_equal galleries(:gallery_with_update).id, @gallery.id
  end
 
end

Railscasts - Free Ruby on Rails Screencasts

Re: Using fixture names

cheers, ryan.

I should get my hands on a copy of the new book, I guess.