# Unit testing and activerecord (mis)behavior

**URL:** <https://www.ruby-forum.com/t/unit-testing-and-activerecord-mis-behavior/185119>\
**Category:** Rails\
**Created:** [March 14, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/unit-testing-and-activerecord-mis-behavior/185119 "2010-03-14T13:09:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 14, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/unit-testing-and-activerecord-mis-behavior/185119/1 "2010-03-14T13:09:32Z")

</div>

In my active record model I have declared a ‘before\_save’ callback, that  
populates other fields before saving the data. However I am finding all  
my unit test cases are breaking because it seems the ‘before\_save’  
callback is not getting called during a unit test run? In fact I have to  
provide the missing filed in unit test fixture yml file.

How can I make the model behave the same in the unit test run as it does  
when I create and save a model object from the rails console?

–  
Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 14, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/unit-testing-and-activerecord-mis-behavior/185119/2 "2010-03-14T15:33:30Z")

</div>

Rajinder Y. wrote:

> In my active record model I have declared a ‘before\_save’ callback, that  
> populates other fields before saving the data. However I am finding all  
> my unit test cases are breaking because it seems the ‘before\_save’  
> callback is not getting called during a unit test run? In fact I have to  
> provide the missing filed in unit test fixture yml file.
> 
> How can I make the model behave the same in the unit test run as it does  
> when I create and save a model object from the rails console?

ok the answer just came to me, what I need to do is a post rather than a  
create on the model, make perfect sense!

–  
Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 14, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/unit-testing-and-activerecord-mis-behavior/185119/3 "2010-03-14T14:16:50Z")

</div>

On 14 March 2010 12:08, Rajinder Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my active record model I have declared a ‘before\_save’ callback, that  
> populates other fields before saving the data. However I am finding all my  
> unit test cases are breaking because it seems the ‘before\_save’ callback is  
> not getting called during a unit test run? In fact I have to provide the  
> missing filed in unit test fixture yml file.

Do you mean it is not calling the filter when loading data from the  
fixture file into the db before the test starts? If so I believe that  
is as it should be. Your code is not run when setting up prior to  
running the test.

If you mean that the filter is not being called when save is called  
from your controller during the test then that is a problem, the  
filter should be called in this case.

Colin
