Need serious help - unit testing without a database


#1

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


#2

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