# How to write unit tests with respect to model callbacks?

**URL:** <https://www.ruby-forum.com/t/how-to-write-unit-tests-with-respect-to-model-callbacks/50895>\
**Category:** Rails\
**Created:** [December 22, 2005, 11:44am UTC](https://www.ruby-forum.com/t/how-to-write-unit-tests-with-respect-to-model-callbacks/50895 "2005-12-22T11:44:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frodo\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ce7236/32.png) [@Frodo\_L](https://www.ruby-forum.com/u/Frodo_L)\
**Post date:** [December 22, 2005, 11:44am UTC](https://www.ruby-forum.com/t/how-to-write-unit-tests-with-respect-to-model-callbacks/50895/1 "2005-12-22T11:44:11Z")

</div>

Hello,

I am exploring the “Testing” part of Rails, but It seems some things  
work differently than I thought it would.

Consider the following model:

class Client \< ActiveRecord::Base

has\_one :project

def after\_create  
Project.new(:name =\> self.name, :is\_client =\> true, :client\_id  
=\> self.id).save  
end

def after\_update  
self.project.update\_attributes(:name =\> self.name, :disabled =\>  
self.disabled)  
end

def before\_destroy  
self.project.destroy  
end

end

and the following test:

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

class ClientTest \< Test::Unit::TestCase  
fixtures :clients

def setup  
@client = Client.find(1)  
end

def test\_create  
assert\_kind\_of Client, @client  
assert\_equal 1, @client.id  
assert\_equal clients(:elasto\_client).name, @client.name  
end

def test\_update  
assert\_equal clients(:elasto\_client).description,  
@client.description  
@client.description = “TEST UPDATE DESCRIPTION”  
assert @client.save, @client.errors.full\_messages.join("; ")  
@client.reload  
assert\_equal “TEST UPDATE DESCRIPTION”, @client.description  
end

def test\_destroy  
@client.destroy  
assert\_raise(ActiveRecord::RecordNotFound) {  
Client.find(@client.id) }  
end  
end

larik@somehost $ ruby test/unit/client\_test.rb  
Loaded suite test/unit/client\_test  
Started  
.EE  
Finished in 0.577375 seconds.

1. Error:  
test\_destroy(ClientTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.destroy  
/Users/larik/cvs/correlator/app/models/client.rb:46:in  
`before_destroy' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:348:in`send’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/callbacks.rb:348:in  
`callback' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:320:in`destroy\_without\_transactions’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/transactions.rb:122:in  
`destroy' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:122:in`transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/transactions.rb:91:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:118:in`transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/transactions.rb:122:in  
`destroy' test/unit/client_test.rb:25:in`test\_destroy’

2. Error:  
test\_update(ClientTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.update\_attributes  
/Users/larik/cvs/correlator/app/models/client.rb:42:in  
`after_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:348:in`send’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/callbacks.rb:348:in  
`callback' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:275:in`update\_without\_timestamps’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/timestamp.rb:39:in  
`update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1431:in`create\_or\_update\_without\_callbacks’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/callbacks.rb:249:in  
`create_or_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1226:in`save\_without\_validation’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/validations.rb:698:in  
`save_without_transactions' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:126:in`save’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/transactions.rb:126:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:91:in`transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/transactions.rb:118:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:126:in`save’  
test/unit/client\_test.rb:19:in `test\_update’

3 tests, 4 assertions, 0 failures, 2 errors

It seems like the data for the clients is directly loaded from the  
fixtures files, it doesn’t respect the callbacks defined in the model.  
E.g. creating of the Project data.

My question is, what is the best way of getting “proper” data in the  
database, with respect to the fixtures data and the callbacks defined  
in the model.

–  
Sincerely,

Frodo L.

---

<div class="post-metadata">

**Author:** ![Frodo\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ce7236/32.png) [@Frodo\_L](https://www.ruby-forum.com/u/Frodo_L)\
**Post date:** [December 22, 2005, 1:35pm UTC](https://www.ruby-forum.com/t/how-to-write-unit-tests-with-respect-to-model-callbacks/50895/2 "2005-12-22T13:35:24Z")

</div>

Hi !

2005/12/22, Frodo L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It seems like the data for the clients is directly loaded from the  
> fixtures files, it doesn’t respect the callbacks defined in the model.  
> E.g. creating of the Project data.
> 
> My question is, what is the best way of getting “proper” data in the  
> database, with respect to the fixtures data and the callbacks defined  
> in the model.

Yes, fixtures data is copied from the fixtures file to the DB, sans  
transformation. If you want the callbacks to execute, you’ll have to  
create the data yourself, using ActiveRecord instances.

On the other hand, fixtures are made to be loaded quickly and  
efficiently in the DB. So, just add a project to the projects.yml  
file, and in the fixtures line of the test case, refer to the projects  
fixture as well as the clients fixture.

Hope that helps !
