# How to test database table manipulation methods?

**URL:** <https://www.ruby-forum.com/t/how-to-test-database-table-manipulation-methods/114399>\
**Category:** Rails\
**Created:** [September 12, 2007, 9:25am UTC](https://www.ruby-forum.com/t/how-to-test-database-table-manipulation-methods/114399 "2007-09-12T09:25:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [September 12, 2007, 9:25am UTC](https://www.ruby-forum.com/t/how-to-test-database-table-manipulation-methods/114399/1 "2007-09-12T09:25:27Z")

</div>

I have written methods to create/drop tables and add/remove columns of  
these tables. This methods will return true/nil depending whether an  
error is raised.

I’d like to test these methods, however, when the first time autotest  
runs, it return a normal report, but later it repeats “uninitialized  
constant Test (NameError)”.

This problem may be caused as the test database has been changed by  
those database table manipulation methods, for example, while a table is  
created from the test code itself, the second time this test,which try  
to add a table with the same name, causes test fails.

Any good idea to test those database table manipulation methods?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [September 12, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-test-database-table-manipulation-methods/114399/2 "2007-09-12T16:47:47Z")

</div>

On 9/12/07, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have written methods to create/drop tables and add/remove columns of  
> these tables. This methods will return true/nil depending whether an  
> error is raised.

AR already has methods to create/drop tables, columns, etc. Are you  
re-inventing the wheel?

> I’d like to test these methods, however, when the first time autotest  
> runs, it return a normal report, but later it repeats “uninitialized  
> constant Test (NameError)”.
> 
> This problem may be caused as the test database has been changed by  
> those database table manipulation methods, for example, while a table is  
> created from the test code itself, the second time this test,which try  
> to add a table with the same name, causes test fails.

autotest isn’t expecting DDL to be going on in your tests, so it  
doesn’t do the “rake db:test:prepare” step before running each test  
(or ever, for that matter, which is why you need to run it yourself  
after a migration, etc.). That would really slow autotest down.

I guess the short answer would be to use the setup and/or teardown  
methods in your test cases to make sure the database is in a known  
state.
