9

ApplicationController:

class ApplicationController < ActionController::Base
  before_filter :authenticate_user!

  protect_from_forgery
end

DashboardsController:

class DashboardsController < ApplicationController
  def index
  end

end

DashboardsControllerSpec:

require 'spec_helper'
describe DashboardsController do
  include Devise::TestHelpers

  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end
end

Result:

Failure/Error: get 'index'
     NoMethodError:
       undefined method `authenticate_user!' for #<DashboardsController:0x007fef81f2efb8>

Rails version: 3.1.3

Rspec version: 2.8.0

Devise version: 1.5.3

Note: I also created support/deviser.rb file but that does not help. Any ideas?

1
  • 1
    The devise Wiki provides a few different ways to integrate devise with rspec. Commented Jan 11, 2012 at 12:40

4 Answers 4

13
require 'spec_helper'
describe DashboardsController do
  before { controller.stub(:authenticate_user!).and_return true }
  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end
end

Update:

Using above syntax with latest rspec will give below warning

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from  `block (2 levels) in <top (required)>'.

Use this new syntax

  before do
     allow(controller).to receive(:authenticate_user!).and_return(true)
   end
Sign up to request clarification or add additional context in comments.

3 Comments

I found the controller.stub(:authenticate_user!).and_return true line very helpful. However, the include Devise::TestHelpers line is unnecessary and misleading. No methods from Devise::TestHelpers are used here, and one reason to stub authenticate_user! in the first place is to avoid a coupling with Devise's machinery.
@evanrmurphy yeap, you are compeltely right, dunno what this include doing in my answer, i will remove it :)
OMG, thank you for this. I tried everything and nothing was working. This is the easiest and fastest solution!
7

Is your model name something other than User? If it's e.g. Admin, then you need to change your filter to:

before_filter :authenticate_admin!

This bit me for a while; I started with User as my model, and later decided to add Devise to a model named Member instead, but I left the original :authenticate_user! in my controller and kept getting that error when running RSpec.

Comments

3

Looks like the best way to do this is the following in your spec_helper.rb file:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

See the rspec wiki for more details.

Comments

1

In my case I had forgotten that I commented out the devise_for line in my routes.rb file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.