# Need serious help - unit testing without a database

**URL:** <https://www.ruby-forum.com/t/need-serious-help-unit-testing-without-a-database/55179>\
**Category:** Rails\
**Created:** [December 31, 2005, 12:58am UTC](https://www.ruby-forum.com/t/need-serious-help-unit-testing-without-a-database/55179 "2005-12-31T00:58:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 31, 2005, 12:58am UTC](https://www.ruby-forum.com/t/need-serious-help-unit-testing-without-a-database/55179/1 "2005-12-31T00:58:38Z")

</div>

I’ve created a model, let’s just call it Person  
class Person  
attr\_accessor :first\_name, :lastname

def full\_name  
@first\_name + " " + @last\_name  
end  
end

I’m using this in my Rails application. My Rails application does not  
use a database of any kind, and this model is obviously not a subclass  
of ActiveRecord::Base. I can’t unit test this class though because  
Rails does all kinds of crap with setting up DB connections, loading  
fixtures, etc. I’ve tried to get around this a number of ways, but  
nothing’s working.

I’ve made a couple posts on this, with no response. I know I’m not  
the first person in the world to create an app that doesn’t need a  
database. I don’t want to scrap this whole project entirely, but I  
have a very tough time coding if I can’t write tests, it’s too easy  
for stuff to get out of control.

Can someone please give me some pointers on using Rails without a DB?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 31, 2005, 2:11am UTC](https://www.ruby-forum.com/t/need-serious-help-unit-testing-without-a-database/55179/2 "2005-12-31T02:11:10Z")

</div>

Hi,

You are probably going to need to give more detail if you want to get  
a good answer to your question. If you want to remove database access  
for a particular testcase you just need to remove the fixtures  
directive at the top of the class AFAIK.

If you want to use the rake tasks defined by rails to run the tests  
then you may need to do a bit more - or alternatively you can just  
write your own tasks. In one of my projects I don’t want the I have  
the following in lib/tasks/db.rake and it means the test test\_\* taks  
do not try to prepare the database.

module Rake  
class Task  
def remove\_prerequisite(task\_name)  
name = task\_name.to\_s  
@prerequisites.delete(name)  
end  
end  
end

Rake::Task.lookup(:test\_units).remove\_prerequisite(:prepare\_test\_database)  
Rake::Task.lookup(:test\_functional).remove\_prerequisite(:prepare\_test\_database)

Rake::Task.lookup(:test\_units).enhance([:environment])  
Rake::Task.lookup(:test\_functional).enhance([:environment])

HTH.

–  
Cheers,

Peter D.

Blog: [http://www.RealityForge.org](http://www.RealityForge.org)
