所有Rubytesting提升:nil:NilClass的未定义方法`authenticate'

我的大多数testing都提出以下,我不明白为什么。 所有的方法调用都会引发“validation”错误。 我已经检查了代码是否有一个称为“authentication”的方法,但没有这样的方法。

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)>' 

该项目可以在那里find=> https://github.com/agilepandas/enki如果你想运行你自己的testing。

@MatthewClosson在Twitter上回答了这个问题

@jeffehh您需要按照https://github.com/plataformatec/devise#test-helpers中的说明创build一个spec / support / devise.rb文件,以包含devise test helpers #ruby

再次感谢。

我知道你正在使用Rspec,但你可以用Test::Unit解决这个问题。 你只需要添加devisetesting助手到test/test_helper.rb

 class ActiveSupport::TestCase include Devise::TestHelpers end 

上面的答案对我来说不起作用(RSpec 3.1)

请参阅https://stackoverflow.com/a/21166482/1161743,了解适用于我的解决scheme。;

在设置variables之前,您需要注销一个匿名用户:

 before :each do sign_out :user end 

在RSpec

正如Jeffrey W.在上面的回答中提到的 – >将其设置给所有的控制者:

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

但是,如果这只与一个规范相关,则不一定需要在所有控制器规范中包含devise助手,只需在那个控制器描述块中明确包含这些助手即可:

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

我的一个项目遇到了同样的问题。 它使用的是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 

从这个 stackoverflow问题的答案中获取灵感,我运行rspec目录的不同组合来找出哪些可能会相互干扰。 最后我发现我在打电话:

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

当我把这一切发生改变为:

 before(:each) do : end 

所有规格都没有失败通过:

 undefined method `authenticate' for nil:NilClass 

我希望这对别人有帮助。

如果你正在使用视图规范,你可以存根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 

它看起来有一些更新的源代码。 ApplicationController指定需要有一个authenticate_user! filter在任何请求前运行。 这个线程提供了一些关于它的问题的背景:

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

本质上, authenticate_user! function是Rails 3的一部分(使用新的devisefunction,我知之甚less)。 如果应用程序找不到用户模型(无论是因为命名空间问题还是其他原因),那么该方法将失败。 您链接到的“enki”应用程序现在是一个Rails 3应用程序。 在转换过程中可能会遇到一些成长的痛苦。

Ruby告诉你,那个方法#authenticate还没有被定义nil 。 你可以很容易地做到这一点:

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

错误将消失。