# Behaviour on ActiveRecord in unit-tests

**URL:** <https://www.ruby-forum.com/t/behaviour-on-activerecord-in-unit-tests/124707>\
**Category:** Rails\
**Created:** [January 1, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/behaviour-on-activerecord-in-unit-tests/124707 "2008-01-01T13:54:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [January 1, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/behaviour-on-activerecord-in-unit-tests/124707/1 "2008-01-01T13:54:01Z")

</div>

Hello,

sadly I’ve some trouble understanding the behaviour of AR in a unit  
test, in  
which an object is destroyed, but not destroyed within the database

Test is here:  
[http://pastie.caboo.se/133741](http://pastie.caboo.se/133741)

The background is easy to explain:  
We’ve contacts and contact\_combies within our application.  
A contact is a human contact (business contact, etc.), a contact\_combi  
represent a relationship between contacts (usually two, but can be  
more).  
Thus a contact has many contact\_combies, and a contact\_combi has many  
contacts. (classic habtm-relation). contact\_combies are edited embedded  
within the contact-form (as explained in [railscasts.com](http://railscasts.com) episodes 73-75)

contact model is here  
[http://pastie.caboo.se/133746](http://pastie.caboo.se/133746)

contact\_combi model here:  
[http://pastie.caboo.se/133745](http://pastie.caboo.se/133745)

test log is here  
[http://pastie.caboo.se/133740](http://pastie.caboo.se/133740)

Side note: all models include a simple helper setting some  
basic-meta-data:  
[http://pastie.caboo.se/133744](http://pastie.caboo.se/133744)

Btw. test is run with:  
use\_transactional\_fixtures = false  
, but setting it to true doesn’t solve the issue.

I observed:

- Within the application (using it by browser) (dev-env), the code  
works.
- The assertion in line 20 fails: two objects exist, one is expected  
[Parked at Loopia](http://pastie.caboo.se/133737)
- Deleting the combi is “tried”, since the contact\_combi’s before\_detroy  
callback is called (see test log)
- No delete is executed in the database
- Its getting quite absurd, if I execute a second contact.save in my  
test  
(enable line 21 - see test)

test\_save\_delete\_multiple\_contact\_combies(ContactTest):  
TypeError: can’t modify frozen hash

/var/lib/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/base.rb:1978:in  
`[]=’

/var/lib/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/base.rb:1978:in  
`write\_attribute’

/var/lib/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/attribute\_methods.rb:67:in  
`attribute=’

/var/lib/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/base.rb:1858:in  
` **send** ’

/var/lib/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/base.rb:1858:in  
`method\_missing’

/home/jan/aptana-workspace-svn/contacts/app/helpers/save\_mixin\_helper.rb:3:in  
`before\_create’  
…

This implies:

- destroy is exectued on the contact\_combi object and the hash is frozen  
by  
active record.
- But why doesn’t these changes appear in the database - is there  
anything I  
can do to make 'em appear?

Thanks,  
Happy 2008!  
Keep smilöing  
yanosz
