n.times { code! }

Thursday, October 11, 2007

UnitRecord and rails 2.0 PR

I've been reading about another way of testing, and I thought that I should give it a try.
The problem is that when you setup all the needed steps, UnitRecord complains about ActiveRecord trying to connect to the database:


[...]
RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests.
[...]

This is due to some changes in rails trunk, that added a "reset_cache" method that checks for an ActiveRecord connection.

In order to avoid this, you can change your unit_test_helper.rb from this (as shown in UnitRecord documentation):

require File.dirname(__FILE__) + "/../test_helper"
require "unit_record"
ActiveRecord::Base.disconnect!

to this:

require File.dirname(__FILE__) + '/../test_helper.rb'
require 'unit_record'

module UnitRecord
module DisconnectedFixtures
def disconnect!
(class << self; self; end).class_eval do
def create_fixtures(*args); end
def reset_cache(*args); end
end
end
end
end

ActiveRecord::Base.disconnect!

That will take care of errors.
Happy testing!

3 comments:

topfunky said...

Very helpful! This fixed the majority of 40 unit test errors in my upgrade to Rails 2.

nic said...

Does this work with the released version of Rails 2? I'm still getting problems with it saying ActiveRecord is disconnected.

Papipo said...

I'm not sure, but I think that the latest unit_record release added support for rails 2.0.

You should try that. If it does not work, leave me a note here and I'll try to fix it.