# Unit test question!

**URL:** <https://www.ruby-forum.com/t/unit-test-question/106674>\
**Category:** Rails\
**Created:** [June 13, 2007, 6:25am UTC](https://www.ruby-forum.com/t/unit-test-question/106674 "2007-06-13T06:25:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [June 13, 2007, 6:25am UTC](https://www.ruby-forum.com/t/unit-test-question/106674/1 "2007-06-13T06:25:14Z")

</div>

Hi,

I have a model and I am trying to do the testing. So my test code is  
something like the following

def test\_first  
assert\_nil Person.find(1)  
end

in this I am expecting the test to run against the development  
database as only development database has the table. But when I ran,  
it is failing because the test table is not found. Why is model  
testing is pointing to test database anyway?

Thanks.

---

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [June 13, 2007, 6:39am UTC](https://www.ruby-forum.com/t/unit-test-question/106674/2 "2007-06-13T06:39:15Z")

</div>

On 6/12/07, DBC User [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in this I am expecting the test to run against the development  
> database as only development database has the table. But when I ran,  
> it is failing because the test table is not found. Why is model  
> testing is pointing to test database anyway?
> 
> Thanks.
> 
> >

Why are you expecting it to run against the development database? It  
will run against whatever database is specified for the test  
environment in database.yml. Take a look at  
[http://glu.ttono.us/articles/2006/05/22/guide-environments-in-rails-1-1](http://glu.ttono.us/articles/2006/05/22/guide-environments-in-rails-1-1)  
to understand Rails environments.

Pat

---

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [June 13, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/unit-test-question/106674/3 "2007-06-13T17:01:28Z")

</div>

On 6/13/07, DBC User [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I have a model and I am trying to do the testing. So my test code is  
> something like the following
> 
> def test\_first  
> assert\_nil Person.find(1)  
> end

This particular test isn’t going to work. find(id) will raise an  
exception if the row can’t be found. If it can be found, it will  
return the Person object. It will never return nil.

So you have a couple of options:

1. Use “assert\_raise”:

2. Use find\_by\_id, which _will_ return nil if the row isn’t found:

> in this I am expecting the test to run against the development  
> database as only development database has the table. But when I ran,  
> it is failing because the test table is not found. Why is model  
> testing is pointing to test database anyway?

It’s because your test file includes “test\_helper.rb” (as it should),  
which sets the RAILS\_ENV variable to “test”.

You’re fighting Rails here. If you create an empty test database and  
then run “rake test”, it will copy the development structure to the  
test database and run the tests over there for you.

The reason for a separate test database is that you want a controlled  
set of data to work with. The data in your development database is  
constantly changing, so your test results wouldn’t be stable.

---

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [June 14, 2007, 5:36am UTC](https://www.ruby-forum.com/t/unit-test-question/106674/4 "2007-06-14T05:36:22Z")

</div>

DBC User wrote:

> testing is pointing to test database anyway?  
> Because manual and automated tests should use different databases.

When you manually test, you need a generally sloppy set of records to  
noodle  
around with. Suppose you set up a given Person record, such as “Mr.  
Frootloop”. Each time you change the code and manually test, you want  
the  
same record to come back in the same state as you left it.

You should run your automated tests much more often than you manually  
test.  
Your automated should have an exact set of data fixtures, and you don’t  
need  
the tests to die just because you manually changed a record. So you need  
your tests to erase the entire database each time they run.

Put these rules together, and you need two databases. Your “manual test  
fixture”, Mr Frootloop, will not disappear just because you ran the  
automated tests.

Tip: Configure the automated tests to run automatically, each time you  
change the code. Then get in the habit of saving your files  
deliberately,  
and trying to predict the result of each test run. If you guess wrong,  
you  
can always Undo your last edit until the tests pass again. This is a lot  
better than endlessly debugging!

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
