# How to disable database setup / teardown in unit tests?

**URL:** <https://www.ruby-forum.com/t/how-to-disable-database-setup-teardown-in-unit-tests/162898>\
**Category:** Rails\
**Created:** [March 15, 2009, 11:44am UTC](https://www.ruby-forum.com/t/how-to-disable-database-setup-teardown-in-unit-tests/162898 "2009-03-15T11:44:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@robb](https://www.ruby-forum.com/u/robb)\
**Post date:** [March 15, 2009, 11:44am UTC](https://www.ruby-forum.com/t/how-to-disable-database-setup-teardown-in-unit-tests/162898/1 "2009-03-15T11:44:44Z")

</div>

Hi,

My app uses a read-only database — what I need to test is the  
parsing code. I’d like to be able to copy the development database to  
test, and run a series of unit tests based on it.

But I can’t see how to stop the test system from clearing the data out  
for each test.

Any way to do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![robb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@robb](https://www.ruby-forum.com/u/robb)\
**Post date:** [March 15, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/how-to-disable-database-setup-teardown-in-unit-tests/162898/2 "2009-03-15T13:27:31Z")

</div>

There’s a :purge task in databases.rake in  
vendor/rails/railties/lib/tasks.  
You’ll wan to find a way to avoid calling that when running tests.  
I’m interested to see what ideas others might share.

Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![robb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@robb](https://www.ruby-forum.com/u/robb)\
**Post date:** [March 15, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/how-to-disable-database-setup-teardown-in-unit-tests/162898/3 "2009-03-15T14:44:35Z")

</div>

Craig D. wrote:

> There’s a :purge task in databases.rake in  
> vendor/rails/railties/lib/tasks. You’ll wan to find a way to avoid  
> calling that when running tests.
> 
> I’m interested to see what ideas others might share.

Leave your test database read-write, and load it up with the  
test/fixtures  
system. Then, in setup(), use Mocha to mock your .save methods (and  
.update, and  
whatever internal methods they all call), and raise a fault if anyone  
calls them.

Always think of mocks as a way to “change reality” - to make random dice  
crooked, or to change the clock time. Alternately…

Switch the DB to “read-only” in setup() and turn that off in  
“tear-down”.

Either way, experiment by trying to .save from a scratch test, to ensure  
the  
system correctly blows up in your face. The point is to make sure at  
production  
time it does not!

–  
Phlip
