<%= NewRelic::Agent.browser_timing_header rescue "" %>

David van Geest

Software, Life, and Stuff I Couldn't Find on the Internet

Hacking session maintenance with Authlogic and Factory Girl

| Comments

I use Authlogic to provide authentication for a couple Rails projects. I also use Factory Girl as a fixtures replacement.

When FG creates a User for you, a new UserSession automatically gets created. This is by virtue of Authlogic’s automatic session maintenance, which can be pretty handy. However, in my tests I want complete control over the UserSession. Just because I created a new user, doesn’t mean I want that user logged in. In many cases, I don’t want the user to be logged in, and this behavior was causing my tests to fail.

To work around this behaviour, I ended up adding this to my User factory:

after_create do |u|
  u.class.maintain_sessions = false

That quashed the autogenerated UserSession. There might be a cleaner method somewhere, but so far this is doing the trick for me.