臭名昭着的AbstractController :: ActionNotFound - Devise + Rails

时间:2011-11-12 04:16:04

标签: ruby-on-rails rspec controller devise

所以我已经阅读了如何解决这个问题:

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

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

但是我在哪个文件下添加这些更改是我的问题:

在我的

的rspec文件夹下
  

registrations_controller

我试过这个

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

哪个不起作用,任何帮助改变特定文件以使这项工作将不胜感激。

修改

所以我也试过了 -

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

所以我创建了一个包含spec / support的文件夹,并创建了一个名为 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

我的 registrations_controller 现在是这样的:

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

我在rspec中有其他控制器我需要更换每一个吗?或者我对在何处进行更改感到困惑。

1 个答案:

答案 0 :(得分:9)

只需使用您尝试的第一个版本,但将before块移到第一个描述块中,如下所示:

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