Docunext


Rspec Rails 3 and AuthLogic

February 13th, 2011

I'm pleased I was able to get Rspec, Rails 3, and AuthLogic working, and to make it easier to do next time, I'm jotting down my notes here.

The trick for me was to figure out how to use Factory Girl, which is a very nice replacement for fixtures, in my humble opinion.

First off, let me clarify I didn't figure this out on my own. I found some very helpful hints in the usual places: StackOverflow, Ruby on Rails mailing lists, etc.

I created a valid_user factory:

Factory.define :valid_user, :class => User do |u|
  u.login "User"
  u.password "password"
  u.password_confirmation "password"
  u.email "user@example.com"
  u.single_access_token "kh3h93hfkhfl4hg8"
end

Then I included and activated AuthLogic in spec/spec_helper.rb:

require "authlogic/test_case"
include Authlogic::TestCase

At the start of each controller spec, I then included this:

  before(:each) do
    activate_authlogic
    @user = Factory.create(:valid_user)
    UserSession.create(@user)
  end

The controller tests were easy to debug, but the views took a little more work.

I ran into a similar issue, and was surprised to learn that Rspec does some impressive inference from the describe argument. For example:
require 'spec_helper'

describe "bills/payments/edit.html.erb" do
  it "Renders payment form" do
    assign(:payment, stub_model(Payment))
    render
  end
end

Due to some evolutionary controller / view names, this test originally had:

describe "bills/payment/edit.html.erb" do

that really messed everything up, and even if I set the :template after render, it was unable to find a referenced partial. Fixing up the path in the describe statement fixed it all.

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020