我正在使用设计进行用户身份验证。当我尝试编写跟踪项目的测试(功能正常)时,我收到以下错误
ERROR["test_should_be_valid", FollowRelationshipTest, 0.245433]
test_should_be_valid#FollowRelationshipTest (0.25s)
NoMethodError: NoMethodError: undefined method `env' for nil:NilClass
但是,如果我从test / test_helper.rb中删除include Devise::TestHelpers
,则不会出现此类错误。
然而,这会引入新的错误,例如
ERROR["test_should_create_project", ProjectsControllerTest, 0.331449]
test_should_create_project#ProjectsControllerTest (0.33s)
NoMethodError: NoMethodError: undefined method `authenticate' for nil:NilClass
app/controllers/projects_controller.rb:28:in `create'
test/controllers/projects_controller_test.rb:21:in `block (2 levels) in <class:ProjectsControllerTest>'
test/controllers/projects_controller_test.rb:20:in `block in <class:ProjectsControllerTest>'
app/controllers/projects_controller.rb:28:in `create'
test/controllers/projects_controller_test.rb:21:in `block (2 levels) in <class:ProjectsControllerTest>'
test/controllers/projects_controller_test.rb:20:in `block in <class:ProjectsControllerTest>
这些新错误似乎源于在视图中调用current_user等方法(例如<%= current_user.email if current_user %>
会产生错误)
我想知道的是如何能够摆脱错误NoMethodError:nil的未定义方法`env':NilClas,同时仍然能够访问由Devise :: TestHelpers引入的方法
FollowRelationshipTest如下
require 'test_helper'
class FollowRelationshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
def setup
@follow_relationship = FollowRelationship.new(follower_id: 1, project_id: 1)
end
test "should be valid" do
assert @follow_relationship.valid?
end
end
和ProjectsControllerTest与使用scaffolding命令生成的内容相同
答案 0 :(得分:1)
很抱歉迟到的回复,但万一其他人偶然发现:
Devise::TestHelpers
只应包含在控制器测试中,因此您需要将其添加到ActionController::TestCase
,方法是将其添加到测试助手中:
class ActionController::TestCase
include Devise::TestHelpers
end