Using Rails without a database
Published by peter December 4th, 2007 in rails, ruby.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
-
ENV["RAILS_ENV"] = "test"
-
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
-
require 'application'
-
require 'test/unit'
-
require 'action_controller/test_process'
-
require 'action_web_service/test_invoke'
-
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
-
[:test_units, :test_functional, :recent].each do |name|
-
Rake::Task[name].prerequisites.clear # I replaced 'Task.lookup(name)' by Task[name]
-
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 '
-
# Skip frameworks you ' re not going to use
-
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:
-
bash>rake test
-
(in /Users/peter/Development/rails/tttt)
-
/opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb"
-
/opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb"
-
No such file or directory - /Users/peter/Development/rails/tttt/config/../config/database.yml
-
...
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”
Please Wait
Leave a Reply