# Help needed in UNIT Testing

**URL:** <https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453>\
**Category:** Rails\
**Created:** [September 29, 2006, 9:44am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453 "2006-09-29T09:44:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 9:44am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/1 "2006-09-29T09:44:00Z")

</div>

hi guys,

I needed help in Unit test. i just try to save new data intomy test  
database.  
but save method does not works for me. the result shows here

def test\_new\_avail  
avail = Avail.new(:vendor\_id =\> 1,  
:last\_mod\_by =\> ‘Murali’,  
:updated\_at =\> '2006-08-17 13:14:59)  
assert avail.valid?  
assert avail.save  
end

is not true  
for both assert.

it causes failures not errors while testing.

Plz tell me how to save data from my test class. i need to save data  
from this method not from my fixture.

advance Thx

regards,  
Narayana.

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 10:51am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/2 "2006-09-29T10:51:35Z")

</div>

Narayana K. wrote:

> hi guys,
> 
> I needed help in Unit test. i just try to save new data intomy test  
> database.  
> but save method does not works for me. the result shows here
> 
> def test\_new\_avail  
> avail = Avail.new(:vendor\_id =\> 1,  
> :last\_mod\_by =\> ‘Murali’,  
> :updated\_at =\> '2006-08-17 13:14:59)  
> assert avail.valid?  
> assert avail.save  
> end
> 
> is not true  
> for both assert.
> 
> it causes failures not errors while testing.
> 
> Plz tell me how to save data from my test class. i need to save data  
> from this method not from my fixture.

> advance Thx
> 
> regards,  
> Narayana.

If your Avail model has an association with a Vendor model then you  
can’t associate them by setting avail.vendor\_id = 1. You need to do it  
the same way you would in regular Rails code.

def test\_new\_avail  
avail = Avail.new(:last\_mod\_by =\> ‘Murali’,  
:updated\_at =\> '2006-08-17 13:14:59)

avail.vendor = Vendor.find\_by\_id(1)

assert avail.valid?  
assert avail.save  
end

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 10:54am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/3 "2006-09-29T10:54:03Z")

</div>

Jonathan T. wrote:

> If your Avail model has an association with a Vendor model then you  
> can’t associate them by setting avail.vendor\_id = 1. You need to do it  
> the same way you would in regular Rails code.
> 
> def test\_new\_avail  
> avail = Avail.new(:last\_mod\_by =\> ‘Murali’,  
> :updated\_at =\> '2006-08-17 13:14:59)
> 
> avail.vendor = Vendor.find\_by\_id(1)
> 
> assert avail.valid?  
> assert avail.save  
> end

I forgot to mention, you’ll need to make sure you load your vendor  
object into the database using a fixture first. That call to find\_by\_id  
could probably use some asserting as well.

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 11:03am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/4 "2006-09-29T11:03:36Z")

</div>

Hi!

> assert avail.valid?

one trick I find convenient to get more accurate information in that  
case is  
to do something like:

assert avail.valid?, “The model is invalid:  
#{avail.errors.full\_messages}”

hth

Thibaut

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/5 "2006-09-29T12:17:05Z")

</div>

Thibaut BarrÃ¨re wrote:

> > one trick I find convenient to get more accurate information in that case  
> > is to do something like:
> > 
> > assert avail.valid?, “The model is invalid: #{avail.errors.full\_messages}”
> 
> Unless you use : assert\_valid 🙂  
> hi,  
> Thx for Both Guys.  
> now  
> assert avail.valid?  
> assert avail.save  
> NO Errors & No Failures.

It passes the both the test, now the problem is, it does not save data  
into the test database. From fixtures to database, transaction takes  
place successfully.  
but for save method the transaction does not take place.

Plz help me to solve this problem.

advance Thx

regards,  
Narayana.

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/6 "2006-09-29T13:20:04Z")

</div>

Narayana K. wrote:

> Thx for Both Guys.  
> Plz help me to solve this problem.
> 
> advance Thx
> 
> regards,  
> Narayana.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

A normal test cycle runs like this…

setup

1. clear the DB
2. load the data from fixtures into the DB

run tests  
3. start a transaction  
4. run the test  
5. rollback the transaction  
6. lather, rinse, repeat (from 3)

So if you save data to the DB from one test, you won’t see any trace of  
it in the DB when you are done.

If you so desire, you can load all of your fixtures into your test db  
by using  
rake db:fixtures:load

\_Kevin

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/7 "2006-09-29T16:16:31Z")

</div>

Jonathan,

Why can’t you manually set vendor\_id?

I’ve done it several times when I wanted to avoid loading the  
associated record.

The only problem I can see is where you need to create the record and  
immediately use the association…

No problems yet - but I want to make sure I’m not shooting myself in  
the foot.

Starr

---

<div class="post-metadata">

**Author:** ![narayana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@narayana](https://www.ruby-forum.com/u/narayana)\
**Post date:** [September 29, 2006, 11:17am UTC](https://www.ruby-forum.com/t/help-needed-in-unit-testing/70453/8 "2006-09-29T11:17:37Z")

</div>

> one trick I find convenient to get more accurate information in that case  
> is to do something like:
> 
> assert avail.valid?, “The model is invalid: #{avail.errors.full\_messages}”

Unless you use : assert\_valid 🙂
