#62 ✓ resolved

Models are not initialised correctly in tests in Merb 0.9.1

Reported by Farrel | March 2nd, 2008 @ 04:47 PM

I'm using Sequel as my ORM and Test::Unit as my testing framework and have the following model:

class Item < Sequel::Model
 validates do
   presence_of( :name, :uuid, :vendor_id, :vendor_reference, :amount, :currency_code )
   uniqueness_of( :uuid )

 def initialize( *args )
   super( *args )
   self.uuid = UUID.random_create.to_s

As you can see I overwrite initialize to set up the UUID when the model is instantiated. In my tests I have the following method which

sets up an Item:

def generate_valid_new_item
   item = Item.new do |i|
     i.name = random_item_name
     i.description = random_description
     i.vendor_id = random_vendor_id
     i.vendor_reference = random_vendor_reference
     i.amount = random_amount
     i.currency_code = random_currency_code
     i.image_url = random_homepage_url
   assert( item.valid?, item.errors.full_messages )

The problem is that when it gets to that assert on the 3rd last line it fails because item.uuid (which should exist) is nil.

During debugging I tried to raise an exception manually in the Item initialize method and no exception was raised which indicates that somehow my custom initialize method is not being called.

The strange thing is that this only happens when I run my unit tests. If I run 'merb -i' (even with -e test) I can initialise an Item correctly as this merb -i session shows:

farrel@nicodemus ~/Projects/boxen $ merb -i
 ~ loading gem 'merb_sequel' from config/init.rb:30 ...
 ~ loading gem 'merb_test_unit' from config/init.rb:41 ...
 ~ Loaded DEVELOPMENT Environment...
 ~ Connecting to the 'boxen_dev' database on 'localhost' using 'postgres' ...
 ~ Compiling routes...
 ~ loading gem 'password' from config/init.rb:58 ...
 ~ loading gem 'validation_extensions' from config/init.rb:59 ...
 ~ loading gem 'uuidtools' from config/init.rb:60 ...
 ~ Using 'share-nothing' cookie sessions (4kb limit per client)
irb(main):001:0> item = Item.new do |i|
irb(main):002:1*   i.name = "Item 1"
irb(main):003:1>   i.description = "It's big"
irb(main):004:1>   i.vendor_id = 5
irb(main):005:1>   i.vendor_reference = "1234"
irb(main):006:1>   i.amount = 2000
irb(main):007:1>   i.currency_code = "USD"
irb(main):008:1>   i.image_url = "http://tempuri.org/img"
irb(main):009:1> end
=> id: 
uuid: f5f198b7-56d4-43ea-a464-60057ea99fec
name: Item 1
description: It's big
vendor_id: 5
vendor_reference: 1234
amount: 2000
currency_code: USD
image_url: http://tempuri.org/img

This was working fine with Merb 0.9 and Sequel has not changed so I'm not sure what is going wrong.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket