Jan 04 2007

Checking that it is true that true is true should not be hard!

Published by Laurie Tagged as:

I spent most of the last day or so playing with testing in ruby on rails. It should be pretty easy, after all rails has testing built in, right? Well that’s what I thought, and I was right, but only to a point.

Starting off with a simple example

class ItemTest < Test::Unit::TestCase
def test_truth
assert true
end
end

Not much to go wrong there. Check if true is in fact true, and if it is, say its true. Always best to start with a trivial example - something that clearly will work. Of course, this is the real world. Did it work? Did it hell!

Instead I get printed a nice little line telling me that there is an error in testing.rake, line 35. Looking at line 35, it is a pretty simple little line: “If there was an error, show error message”. Great, this is going well.

I probably should tell you a bit of background here. I’m not trying to use a database. All my data is going to be obtained from a nice sexy web service, in a way that is really quite cool, but apparently beyond the expected uses of RoR. James Andrews pointed me in the direction of recipe 14 of Rails Recpies, and a blog post on databaseless rails application testing. Both were of course out of date. So here is what you need to do, of course, this only works for ruby 1.1.6 for sure, and revision of 5832 of edge rails. Who knows if it will work for any other version.

1) Change the file test/test_helper.rb so it reads as follows:

ENV["RAILS_ENV"] = “test”
require File.expand_path(File.dirname(__FILE__) + “/../config/environment”)
require_dependency ‘application’
silence_warnings { RAILS_ENV = “test” }

require ‘test/unit’
require ‘action_controller/test_process’
require ‘action_controller/integration’
require ‘action_web_service/test_invoke’
require ‘breakpoint’

class Test::Unit::TestCase
# Add more helper methods to be used by all tests here…
end

This basically just removes any reference to databases from the test framework

2) Add a file in lib/tasks called something like “remove_database_dependency.rake” and put the following in the file

["test:units", "test:functionals", "test:integration", "test:recent", "test:uncommitted"].each do |name|
Rake::Task[name].prerequisites.clear
end

This goes through all the rake testing tasks, and removes all of their prerequisites (which in my version of rails only consists of loading the database)

There you go, after all of that, you can now tell that true is true!

Trackback URI | Comments RSS

Leave a Reply

For spam filtering purposes, please copy the number 8201 to the field below: