Wednesday 25 August 2010

Testing with devise

Today I ran across a problem getting tests to pass with Devise. There were in fact two issues. First, in rails 3 the Gemfile does not stipulate the order in which gems are loaded and this can mess things up a bit. Especially with mocha, which I use for mocking and stubbing.

This post describes the issue.

To solve the problem I put

require 'mocha'
Bundler.require(:test)
include Devise::TestHelpers



at the end of test/helper.rb

But the sets still didn't pass ....

The other problem to which the solution was more obvious when I actually thought about it was because I am using confirmable. So the setup needed to be:

def setup
@user = users(:one)
sign_in @user
@user.confirm!
end

Friday 20 August 2010

i18n in rails 3

Well it's been a long time, but I'm going to make an effort to do more blogging now that rails 3 is almost here - partly to keep track of what I need to remember - but also because it might be useful to others.

I'm upgrading a big app to rails 3 before we go live with it - and of course there are a few glitches. Mostly these are to do with gems and plugins that don't work - more on them when I get to them. But the first big hiccup was internationalization. The app I'm upgrading is fully internationalized - but when I got the front page up i got things like

translation missing: 'en'::character varying, devise, sessions, user, signed_in

instead of the nice translations i was expecting.

The cause seems to lie in the I18n gem - where it gets the configuration object


class << self
# Gets I18n configuration object.
def config
Thread.current[:i18n_config] ||= I18n::Config.new
end


I patched this by dropping a file in the lib directory and requiring it from application.rb

module I18n
class << self
def config
# don't pick up the config object from the current thread -
# it returns 'en'::character varying
I18n::Config.new
end
end
end



My guess is that this might slow things down a touch - creating a new config object instead of using an existing one - but it works for me so far