首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >所有Ruby测试都会引发: nil:NilClass的未定义方法‘`authenticate’

所有Ruby测试都会引发: nil:NilClass的未定义方法‘`authenticate’
EN

Stack Overflow用户
提问于 2010-11-30 05:05:29
回答 7查看 39.5K关注 0票数 132

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

代码语言:javascript
复制
  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,如果你想自己运行测试的话。

EN

回答 7

Stack Overflow用户

发布于 2012-03-29 11:53:35

我知道您使用的是Rspec,但是使用Test::Unit也会遇到同样的问题。您只需将设计的测试助手添加到test/test_helper.rb

代码语言:javascript
复制
class ActiveSupport::TestCase
  include Devise::TestHelpers
end
票数 74
EN

Stack Overflow用户

发布于 2015-01-14 19:19:05

在RSpec中

正如Jeffrey W.在他的answer above ->中提到的,将此设置为所有控制器:

代码语言:javascript
复制
RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, type: :controller
  # ...
end

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

代码语言:javascript
复制
require 'spec_helper'
describe MyCoolController
  include Devise::TestHelpers

  it { } 
end
票数 9
EN

Stack Overflow用户

发布于 2014-10-10 14:21:33

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

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

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

代码语言:javascript
复制
before :each do
  sign_out :user
end
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4308094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档