使用 Paper Trail,我为我的应用构建了一个更改跟踪元素。在生产中效果很好。一方面是它使用“whodunnit”字段来提取用户名以显示哪个用户进行了更改。
我查看了文档 ( https://github.com/airblade/paper_trail ),我看到了关于 rspec 和 whodunnit 的符号,但我不认为该引用意味着“whodunnit”字段不可用,只是它会是 nil' ed 在 测试之间。
详细信息: 我正在使用 rspec 的测试助手,并且我有 rspec 功能测试设置
"..., versioning: true do"
此外,在运行测试时进行调试时,我可以看到事件/更改的所有其他字段都在那里并保存在数据库中,只有 whodunnit 没有被保存。有趣的是,我只是在测试中遇到问题;生产中没有问题,在那里工作正常,只是在测试中不起作用。当然,我有
before_action :set_paper_trail_whodunnit
设置(在我的应用程序 Controller 中,不是特定的 Controller ,但我移动它只是为了看看它是否有所不同,不出所料,它没有)。
编辑: 我应该提到我检查以确认在测试运行时测试数据库中有一个列“whodunnit”。此外, Controller 中还有一个可用的方法 current_user(正如预期的那样,否则它不会在生产环境中工作)。
最佳答案
我从 PaperTrail issue on Github 中找到了这个
Unless you are running controller and / or integration specs prior to running your query it's likely that the
whodunnit
column is not being populated.
我最终在我的规范中手动设置了我的 whodunnit
PaperTrail.controller_info[:whodunnit] = user.id
这有点违背初衷。但是我在单元规范和功能规范中运行它,所以我认为这是唯一的方法。
关于ruby-on-rails - rspec 功能测试中没有 whodunnit 使用 paper trail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48361949/