运行我的测试时,我一直遇到一个完全无意义的错误:
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','轮胎','〜> 0.4.2'和factory_girl_rails
**修改
想出来了。我可以发布答案
答案 0 :(得分:0)
想出来了。基本上是我们的错误。在我们的spec_helper中,我们试图为我们的测试存根弹性搜索,并使用RestClient gem来实现。当工厂构建器创建一个新的测试对象时,它触发了after_save方法,然后elasticsearch发送了一个post请求,该请求被RestClient部分存根(“RestClient.stub(:post)”)。
修改强>
我发布的更好的stub弹性搜索/ rspec测试的链接已被删除,而Tire现在已被弃用,有利于Karmi的内置测试的新elasticsearch-rails gem。请查看Pivotal labs post有关如何测试elasticsearch查询的信息。