eWizardII
eWizardII

Reputation: 1936

Infamous AbstractController::ActionNotFound - Devise + Rails

So I have read how to solve this problem:

RSpec Test of Custom Devise Session Controller Fails with AbstractController::ActionNotFound

and

http://lostincode.net/blog/testing-devise-controllers

But under which file do I add these changes is my problem:

Under the rspec folder for my

registrations_controller

I tried this

before :each do
  request.env['devise.mapping'] = Devise.mappings[:user]
end

require 'spec_helper'

describe RegistrationsController do

  describe "GET 'edit'" do
    it "should be successful" do
      get 'edit'
      response.should be_success
    end
  end

end

Which didn't work, any help with the specific files to change to make this work would be greatly appreciated.

EDIT

So I also tried -

https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)

so I made a folder with spec/support and made a file called controllers_macros.rb

module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:admin]
      sign_in Factory.create(:admin) # Using factory girl as an example
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = Factory.create(:user)
      user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end

And my registrations_controller is now this:

require 'spec_helper'

describe RegistrationsController do

  describe "GET 'edit'" do
    before :each do
      request.env['devise.mapping'] = Devise.mappings[:user]
    end
    it "should be successful" do
      get 'edit'
      response.should be_success
    end
  end

end

I have other controllers in rspec do I need to change every single one? Or I'm confused on where to make the changes.

Upvotes: 0

Views: 2157

Answers (1)

David Tuite
David Tuite

Reputation: 22643

Just take the first version you tried, but move the before block inside the first describe block like this:

require 'spec_helper'

describe RegistrationsController do
  before :each do
    request.env['devise.mapping'] = Devise.mappings[:user]
  end

  describe "GET 'edit'" do
    it "should be successful" do
      get 'edit'
      response.should be_success
    end
  end
end

Upvotes: 9

Related Questions