【问题标题】:Undefined Method 'Body' for Nil Class - Tire (Elastic Search) / Rspec / FactoryGirl / ActiveRecordNil 类的未定义方法“Body” - 轮胎(弹性搜索)/Rspec/FactoryGirl/ActiveRecord
【发布时间】:2014-04-17 18:04:16
【问题描述】:

在运行我的测试时,我不断遇到一个完全无法描述的错误:

undefined method `body' for nil:NilClass

堆栈跟踪将我指向我用于数据库的 gem 中与 Elastic Search 相关的 1 行,该数据库在两个 Rails 项目之间共享。

undefined method `body' for nil:NilClass
# /home_path/project/project-lib/lib/model.rb:31:in update_or_remove_index'

第31行相关的代码是

after_save :update_or_remove_index

def update_or_remove_index
  if self.visible
    update_index
  else
    tire.index.remove self
  end
end

当 FactoryGirl 创建一个模拟对象并保存它,触发 after_save#update_or_remove_index 时,此错误通常发生在规范中。当我存根 :update_or_remove_index 时,测试通过了。

我正在运行 Ubuntu 13.10、Rails '3.2.17'、'tire'、'~> 0.4.2' 和 factory_girl_rails

**编辑

想通了。有空会回复的

【问题讨论】:

    标签: ruby-on-rails rspec factory-bot tire


    【解决方案1】:

    想通了。基本上是我们这边的一个错误。在我们的 spec_helper 中,我们试图为我们的测试剔除 elasticsearch,并使用 RestClient gem 来做到这一点。当 factory builder 创建一个新的测试对象时,它触发了 after_save 方法,然后 elasticsearch 发送一个 post 请求,该请求被 RestClient 部分存根(“RestClient.stub(:post)”)。

    编辑

    我发布到更好的 stub elasticsearch / rspec 测试的链接已被删除,现在不推荐使用 Tyre,取而代之的是 Karmi 的新 elasticsearch-rails gem,它内置了测试。请查看Pivotal labs post,了解如何测试 elasticsearch 查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2023-03-27
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多