views:

355

answers:

2

How would you stub Devise in Rails 3 with rSpec. I have a UsersController and a User model. Both of which are associated with Devise at the moment, I'm writing controller specs and I really am having a hard time with my expectations as the Devise sign_in is really jamming up the works.

Any thing will help.

+2  A: 

You can try mocking the underlying warden (http://wiki.github.com/hassox/warden/) object which devise relies upon, here is a link to some details on how you can accomplish this with RSpec: http://www.michaelharrison.ws/weblog/?p=349 (entry covers some other topics as well, the solution you want is towards the bottom of the page.)

Oscar Duignan
Thanks so much! Just what I've been looking for.
stuartc
The new answer below provides a better way http://stackoverflow.com/questions/3387485/stubbing-devise-in-rspec-and-rails3/3512813#3512813
Khaja Minhajuddin
+4  A: 

I found that it is now pretty easy to do this. There was a problem with rspec2 and devise, but is now solved. I guess you would need to update your gems. Then you can write

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
nathanvda