Forum: Ruby on Rails Need serious help - unit testing without a database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2005-12-31 00:58
(Received via mailing list)
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
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 Peter Donald (Guest)
on 2005-12-31 02:11
(Received via mailing list)
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 Donald

Blog: http://www.RealityForge.org
This topic is locked and can not be replied to.