在Rails Controller测试中访问线程变量失败

我有一个Ruby on Rails 5应用程序,我在其中使用Thread变量来访问用户,以启动我自己的专有日志记录的操作。在进行自动化测试之前,一切正常。

我在clipTobound中有一个模块:

lib/current.rb

在我的module Current thread_mattr_accessor :user end 中,我这样设置当前用户:

app/controllers/application_controller.rb

这样,当我在应用程序中创建/更新/删除实体时,便能够登录当前登录的用户。在around_action :set_current_user [...] def set_current_user Current.user = current_user yield ensure Current.user = nil end 中:

app/models/entity.rb

如上所述,在我尝试运行自动化测试之前,该方法工作正常:

after_create :log_create
before_update :log_update
before_destroy :log_destroy,prepend: true

[...]

def log_destroy
  ApplicationController.helpers.create_change_log([...],Current.user.id)
end

这会导致以下错误消息:

entity_instance = Entity.find([...])
entity_instance.destroy

我向您保证,该错误指示不存在Minitest::UnexpectedError: NoMethodError: undefined method `id' for nil:NilClass 。有人知道为什么会这样吗?

wssfeng8287 回答:在Rails Controller测试中访问线程变量失败

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3151330.html

大家都在问