Question

When running my tests, I keep encountering a completely undescriptive error:

undefined method `body' for nil:NilClass

The stack trace points me to 1 line related to Elastic Search in a gem I use for my database which is shared amongst two rails projects.

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

The code related to line 31 is

after_save :update_or_remove_index

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

This error occurs typically in specs when FactoryGirl creates a mock object and saves it, triggering the after_save#update_or_remove_index. When I stub :update_or_remove_index, the tests pass.

I'm running Ubuntu 13.10, Rails '3.2.17', 'tire', '~> 0.4.2' and factory_girl_rails

**Edit

Figured this out. Will post answer when I can

Was it helpful?

Solution

Figured this out. Basically an error on our end. In our spec_helper we were attempting to stub out elasticsearch for our tests and were using RestClient gem to do so. When factory builder created a new test object, it was triggering the after_save method and then elasticsearch was sending a post request which was getting partially stubbed by RestClient ("RestClient.stub(:post)").

Edit

The link I posted to better stub elasticsearch / rspec tests was taken down and Tire is now being deprecated in favor of Karmi's new elasticsearch-rails gem which has testing built-in. Please check out Pivotal labs post on how to test elasticsearch queries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top