【问题标题】:Rspec - Check for the instance of ActiveRecord::Relation of a specific ModelRspec - 检查特定模型的 ActiveRecord::Relation 实例
【发布时间】:2018-09-05 07:43:06
【问题描述】:

在我们的应用程序中,我们使用了这个

expect_any_instance_of(Order::ActiveRecord_Relation)
  .to receive(:something)

当我们将应用程序升级到 rails 5.2 时,我们收到以下错误

NameError:
   private constant #<Class:0x000055aa351fc9a0>::ActiveRecord_Relation referenced

有没有办法用expect_any_instance_of检查特定模型的ActiveRecord::Relation

同样的https://github.com/rails/rails/issues/30943 出现问题

【问题讨论】:

  • 核心开发有明确的解释,您正在尝试访问私有方法。它不是公共 API 的一部分,所以你不能使用它。
  • 我知道,这就是为什么我要问是否有任何方法可以在不使用Class:&gt;::ActiveRecord_Relation的情况下进行检查
  • 这可能不是最好的测试预期,向我们展示您正在测试的代码,我们或许可以为您提供替代方案

标签: ruby-on-rails rspec rails-activerecord rails-upgrade


【解决方案1】:

试试这个Order.const_get(:ActiveRecord_Relation)。它应该让它在 rails >= 5.2 中工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2016-01-19
    • 2016-05-23
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多