我正在使用内置的 Minitest 为 Rails v5.1 编写集成测试。
这是集成测试类:
require 'test_helper'
class PuppiesEndpointsTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "DELETE puppy" do
marty = people(:marty)
sign_in(marty)
# delete puppies_delete_path(marty.puppies.first.id)
# delete `/api/v1/puppies/destroy/${marty.puppies.first.id}.json`
# delete puppies_path(marty.puppies.first.id)
delete '/api/v1/puppies/destroy/6666.json'
assert_response :success
end
end
上面的所有路由,包括被注释掉的路由,都会导致相同的神秘错误:
Error:
PuppiesEndpointsTest#test_DELETE_puppy:
NoMethodError: undefined method `[]=' for nil:NilClass
test/integration/puppies_endpoints_test.rb:17:in `block in <class:PuppiesEndpointsTest>'
bin/rails test test/integration/puppies_endpoints_test.rb:7
它没有提供堆栈跟踪或任何其他信息来诊断它到底在说什么。我使用 byebug 来调试抛出错误的 delete
行之前的 marty
变量。它显示了预期的关联(固定)记录的小狗数组。
我还在 Controller 操作的最顶部放置了一个 byebug,并且此错误在到达该 byebug 之前使测试失败,因此我认为这几乎排除了操作代码中的任何内容。
这是我运行rake paths
时看到的相关内容:
PATCH /api/v1/puppies/edit/:id(.:format) puppies#update
DELETE /api/v1/puppies/destroy/:id(.:format) puppies#destroy
puppies_create POST /api/v1/puppies/create(.:format) puppies#create
这是我的路线文件中的实际内容:
scope '/api' do
scope '/v1' do
devise_for :people
patch 'puppies/edit/:id' => 'puppies#update'
delete 'puppies/destroy/:id' => 'puppies#destroy'#, as: 'puppies_delete'
post 'puppies/create' => 'puppies#create'
...
我完全不明白我收到此错误的原因/原因。实际代码完全按照预期工作。
我的预感是,可能缺少一个配置变量,该变量没有为测试环境设置(我使用 dotenv gem),但如果错误不会给我任何上下文,我不知道如何跟踪它。
更新
我已使用 Devise 帮助器 sign_in
方法将此问题隔离。当我删除此方法调用时,问题就消失了。
这是有问题的测试类:
require 'test_helper'
class PuppiesEndpointsTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "do stuff" do
...
应用程序/ Controller /api_controller.rb:
class ApiController < ActionController::API
end
也许sign_in
不适用于测试不继承自ActionController::Base的 Controller
我将 Controller 更改为从 ActionController::Base
继承,但没有任何变化。我仍然无法在不出现该错误的情况下使用 sign_in
,但如果我“手动”post
向 Sign_in 端点发送请求,它就可以找到。
更新2 我发现这个 Devise 问题听起来与我的问题相关:https://github.com/plataformatec/devise/issues/2065
最佳答案
看起来我找到了issue 。显然,在rails-api模式下,ActionDispatch::Cookies和ActionDispatch::Session::CookieStore中间件被插入到中间件堆栈的末尾,这在普通Rails模式下不会发生。
因此,这些中间件包含在 Warden::Manager 之后,这会弄乱请求规范中的某些内容。
尝试在test.rb中设置
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore
关于ruby-on-rails - Rails 5 集成测试失败,出现 NoMethodError : undefined method `[]=' for nil:NilClass when using Devise helper sign_in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54979970/