Hi,
I have the following code in a RoR project, models and tests are all in the proper directories:
require File.dirname(__FILE__) + '/../test_helper'
class GalleryTest < Test::Unit::TestCase fixtures :galleries
@gallery = nil
def test_presence_of @gallery = Gallery.new @gallery.title = 'My test gallery' @gallery.nr = 123 image = Image.new(1, 100, 'jpg') @gallery.images = Array.new @gallery.images << image @gallery.save
assert @gallery.valid? end end
class Gallery < ActiveRecord::Base validates_presence_of :title, :nr, :images validates_numericality_of :nr validates_length_of :title, :in => 1..255
has_many :images end
class Image < ActiveRecord::Base validates_presence_of :nr, :sort_nr, :ext validates_presence_of :sort_nr validates_presence_of :ext
belongs_to :gallery
def initialize(nr, sort_nr, ext) @nr = nr @sort_nr = sort_nr @ext = ext end end
When I run the gallery test file, ruby shows me the following error:
test_presence_of(GalleryTest): NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil. c:/program files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_ record/base.rb:1871:in `read_attribute' ... record/transactions.rb:121:in `transaction' c:/program files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_ record/transactions.rb:129:in `save' test/unit/gallery_test.rb:23:in `test_presence_of'
It has something to do with gallery.images, but I can't figure out how to fix this.
Another question (offtopic): Why does it take 10 seconds for the test to start running? This gets annoying when you want to "code a little, test a little".