rspec2 and rails3: controller testing with devise

Devise seems to become the new authentication standard in rails land. Also the last version of the gem is for rails3, so it is very easy to start using it. If you are using rspec2 for your tests, it is also really easy to test your controllers that are authenticated by devise. Suppose you have a DoStuffController like this: [ruby] class DoStuffController < ApplicationController before_filter :authenticate_user! def index end end [/ruby] You need to test it as follows in do_stuff_controller_spec.rb: [ruby] require 'spec_helper' describe DoStuffController do include Devise::TestHelpers before (:each) do @user = Factory.create(:user) sign_in @user end describe "GET 'index'" do it "should be successful" do get 'index' response.should be_success end end end [/ruby] And in your factory, users.rb: [ruby] Factory.define :user do |f| f.email "bla@blabla.com" f.password "blax123" end [/ruby] I am not testing the fact that the redirect will work, under the assumption that that is specific devise functionality which i assume to be tested and working.
Comments
Kirk 2010-09-03 04:36:30 UTC

Hey thanks very much for this, this was exactly what I was looking for great stuff!

Nathan 2010-09-03 10:21:14 UTC

Thanks :)

Add comment

Recent comments

Tags