首页 文章

所有Ruby测试都提出了:nil的未定义方法`authenticate':NilClass

提问于
浏览
128

我的大多数测试都提出了以下内容,我不明白为什么 . 所有方法调用都会引发“authenticate”错误 . 我检查了代码是否有一个名为“authenticate”的方法,但没有这样的方法 .

1) Admin::CommentsController handling GET to index is successful
     Failure/Error: get :index
     undefined method `authenticate!' for nil:NilClass
     # ./spec/controllers/admin/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'


  124) PostsController handling GET for a single post should render show template
     Failure/Error: get :show, :year => '2008', :month => '01', :day => '01', :slug => 'a-post'
     undefined method `authenticate' for nil:NilClass
     # ./app/controllers/application_controller.rb:18:in `set_current_user_for_model'
     # ./spec/controllers/posts_controller_spec.rb:131:in `do_get'
     # ./spec/controllers/posts_controller_spec.rb:140:in `block (3 levels) in <top (required)>'

该项目可以在那里找到=> https://github.com/agilepandas/enki,以防你想自己运行测试 .

8 回答

  • 71

    Ruby告诉你,那个方法 #authenticate 尚未在 nil 上定义 . 您可以通过以下方式轻松完成:

    def nil.authenticate!
      puts "Bingo! Nil is now authentic!"
    end
    

    错误将消失 .

  • 7

    在RSpec

    正如杰弗里W.提到的那样,在他的_318649中 - >将其设置为所有控制器:

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

    但是,如果这只与一个规范相关,那么您不一定需要在所有控制器规范中包含设计帮助程序,您可以在那个控制器描述块中明确地包含这些帮助程序:

    require 'spec_helper'
    describe MyCoolController
      include Devise::TestHelpers
    
      it { } 
    end
    
  • 7

    我在其中一个项目中遇到了同样的失败 . 它使用Ruby 2.0.0-p598,Rails 3.2.21,RSpec 2.99 . 当我一起运行所有规格时,问题就出现了 . 当我单独运行规格时,它们通过了 . 我的spec / spec_helper.rb中包含以下内容:

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

    我在每个失败的spec文件中的第一个描述中添加了以下内容 . 这没有解决问题:

    before :each do
      sign_out :user
    end
    

    也没有:

    after :each do
      sign_out :user
    end
    

    从答案到this stackoverflow问题的灵感,我运行了不同的rspec目录组合,以找出哪些可能会相互干扰 . 最后我发现我在呼唤:

    before() do #note no :each passed to before
      :
    end
    

    当我将所有出现的内容更改为:

    before(:each) do
      :
    end
    

    所有规格都没有失败:

    undefined method `authenticate' for nil:NilClass
    

    我希望这对别人有帮助 .

  • 1

    我'm aware you'正在使用Rspec,但您可以使用 Test::Unit 遇到同样的问题 . 你只需要将设计测试助手添加到 test/test_helper.rb

    class ActiveSupport::TestCase
      include Devise::TestHelpers
    end
    
  • -19

    以上答案对我不起作用(RSpec 3.1)

    请参阅https://stackoverflow.com/a/21166482/1161743以获取适合我的解决方案 .

    在设置变量之前,您需要注销匿名用户:

    before :each do
      sign_out :user
    end
    
  • 189

    @MatthewClosson在推特上回答了这个问题

    @jeffehh您需要创建一个spec / support / devise.rb文件,如https://github.com/plataformatec/devise#test-helpers所示,包含设计测试助手#ruby

    再次感谢 .

  • 0

    如果您正在使用视图规范,则可以使用 current_user 的存根 . 这有效地覆盖了从您的视图调用的 current_user 帮助器返回的任何内容 . 这是rspec-3.2.3的方法:

    RSpec.describe "projects/show", type: :view do
      before(:each) do
        allow(view).to receive(:current_user).and_return(FactoryGirl.create(:user))
      end
    
      it "renders attributes in <p>" do
        render
        expect(rendered).to match(/whatever you want to regex match here/)
      end
    end
    
  • -3

    看起来源代码有一些更新 . ApplicationController指定在任何请求之前需要运行 authenticate_user! 过滤器 . 该主题提供了有关它的问题的一些背景知识:

    http://groups.google.com/group/plataformatec-devise/browse_thread/thread/f7260ebe2d9f7316?fwc=1

    从本质上讲, authenticate_user! 函数是Rails 3的一部分(使用新的 devise 功能,我对其知之甚少) . 如果应用程序无法找到用户模型(由于命名空间问题或任何原因),则该方法将失败 . 您链接到的"enki"应用程序现在是Rails 3应用程序 . 它转换过来可能会经历一些成长的痛苦 .

相关问题