Using Rails without a database

Today I was fiddling with Rails to create a simple web UI on top of some REST services. Therefor I didn't really need (or is it really didn't need) a database server. Just writing and running Rails code without a database isn't a problem. Running unittests (and rspec tests) however is a problem. By default the unittest helpers used by the generated tests try to initialize a database connection.

Rails Recipes to the rescue. Recipe 14 ('Rails without a database') explains how to modify the helpers and environment configuration to get everything to work without a database and ActiveRecord. Kudos for pointing this out to Levi!!.

Getting the tests to work
open /test/test_helper.rb and replace it's content with

RUBY:
  1. ENV["RAILS_ENV"] = "test"
  2. require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
  3. require 'application'
  4. require 'test/unit'
  5. require 'action_controller/test_process'
  6. require 'action_web_service/test_invoke'
  7. require 'breakpoint'

Getting the tests to work with Rake
In this case the solution proposed in Rails Recipes actually didn't work; it referenced a non-existing 'lookup' method on the Rake::Task class. That wasn't to hard to solve.
In /lib/tasks/ create a file called 'clear_database_prerequisites.rake' and insert the following code:

RUBY:
  1. [:test_units, :test_functional, :recent].each do |name|
  2.   Rake::Task[name].prerequisites.clear # I replaced 'Task.lookup(name)' by Task[name]
  3. end

According to Rails Recipes this single line looks up each Task using Rake’s API and clears the Task’s dependencies. Now according to Rails Recipes running 'rake test' should work as well; in my case it didn't I had to disable loading ActiveRecord.

Disabling ActiveRecord altogether
It is possible to disable loading ActiveRecord by making a simple modification to '/config/environment.rb':

RUBY:
  1. # Skip frameworks you ' re not going to use
  2. config.frameworks -= [ :active_record ]

This was a bit complexer then I'd hoped, but I'm glad to be able to write AND run tests again!

Removing database.yml
After doing all of the above I found the following ticket in Rails' issuetracker: http://dev.rubyonrails.org/ticket/7868 which claims that the convention is to remove your database.yml after which Active Record assumes it's inactive. I tried it, but it doesn't seem to work like that:

CODE:
  1. bash>rake test               
  2. (in /Users/peter/Development/rails/tttt)
  3. /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb" 
  4. /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb" 
  5. No such file or directory - /Users/peter/Development/rails/tttt/config/../config/database.yml
  6. ...

but this might be related to my setup. I'll stick with the Rails Recipes solutions for now.


1 Response to “Using Rails without a database”

  1. 1 Aplicação sem banco de dados : Ruby Brasil

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


Rudie alcatraz Stretched Limo Greenland javaone2008 keynote Stage being build in the nearby park Tim Bray introducing the (J)Ruby panel Okke en Rudie pub Java + You on a cab golden_gate_warning_sign Scribbled Sun Logo Hotel room nearby hotel smashmouth Cable Car line sea_lion Acme Anvile at CommunityOne Keynote Community One Keynote Moscone Center - JavaOne
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.