我的大多数测试都提出了以下问题,我不明白为什么。所有方法调用都会引发“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,如果你想自己运行测试的话。
发布于 2012-03-29 11:53:35
我知道您使用的是Rspec,但是使用Test::Unit
也会遇到同样的问题。您只需将设计的测试助手添加到test/test_helper.rb
class ActiveSupport::TestCase
include Devise::TestHelpers
end
发布于 2015-01-14 19:19:05
在RSpec中
正如Jeffrey W.在他的answer above ->中提到的,将此设置为所有控制器:
RSpec.configure do |config|
# ...
config.include Devise::TestHelpers, type: :controller
# ...
end
但是,如果这只与一个规范相关,那么您不一定需要在所有控制器规范中都包含设计帮助器,您只需在一个控制器描述块中显式地包含这些帮助器:
require 'spec_helper'
describe MyCoolController
include Devise::TestHelpers
it { }
end
发布于 2014-10-10 14:21:33
上面的答案对我不起作用(RSpec 3.1)
请参阅https://stackoverflow.com/a/21166482/1161743,以获得适合我的解决方案。
在设置变量之前,您需要注销匿名用户:
before :each do
sign_out :user
end
https://stackoverflow.com/questions/4308094
复制相似问题