# Unit Test and RecordNotSaved

**URL:** <https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524>\
**Category:** Rails\
**Created:** [January 19, 2008, 12:12am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524 "2008-01-19T00:12:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 19, 2008, 12:12am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524/1 "2008-01-19T00:12:33Z")

</div>

I have a Company model that has an after\_create callback that creates  
default roles, users, etc for the new company. In the development  
environment, this all works great. In my company unit test however, an  
ActiveRecord::RecordNotSaved is thrown on the first attempt to create a  
role for the new company (and this is also the first attempt to create  
anything in the after\_create callback). I have added logger statements  
in the code to verify that both the company and new role are valid, and  
they are. Is there something I am missing? If not, is there a better way  
to track down why I am getting a RecordNotSaved?

TIA,  
-Bill

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 19, 2008, 12:30am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524/2 "2008-01-19T00:30:11Z")

</div>

On Jan 19, 2008 9:41 AM, William P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 19, 2008, 12:30am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524/3 "2008-01-19T00:30:12Z")

</div>

What I meant to say was:

Do @company.save and then puts @company.errors to see what errors there  
are.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 19, 2008, 12:37am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524/4 "2008-01-19T00:37:51Z")

</div>

There are no errors. Both the company and the role are valid and the  
company is already saved at this point. I logged a company.inspect and  
it shows new\_record to be false. Both the company and the role return  
true from a call to valid?.

-Bill

Ryan B. wrote:

> What I meant to say was:
> 
> Do @company.save and then puts @company.errors to see what errors  
> there are.
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 19, 2008, 1:44am UTC](https://www.ruby-forum.com/t/unit-test-and-recordnotsaved/126524/5 "2008-01-19T01:44:00Z")

</div>

Nm, I found it. I have an abstract base class that was catching a  
condition and not injecting the error before returning false. Thanks  
anyway Ryan.

-Bill

William P. wrote:

> TIA,  
> -Bill

–  
Sincerely,

William P.
