Search code examples
ruby-on-railssunspot

Sunspot and RSpec how to stub?


Hi I've been trying to get this working, I have a controller that uses Sunspot to search on a model, I used to have a plain sql statement with likes and on my test was expecting to get an array of object. Now that I've started implementing Sunspot I am not sure on how to make a simple test work, I want to basically stub that whenever that controller uses search method it should return an array of object.

Have you guys use Rspec + Sunspot? Any good advice that I can follow on this, maybe I am not using it properly,

PS. I've read https://github.com/sunspot/sunspot/wiki/RSpec-and-Sunspot and it is fairly brief on thef act that I need to use ::Sunspot::Rails::StubSessionProxy.new(::Sunspot.session) class at the Sunspot::Session


Solution

  • I was able to bypass my problem by examining the source code of sunspot more closely, seems that the StubSessionProxy is a dumb implementation with most of the code empty method. First I realize that I should not be trying to stub the search method inside this class, but instead the new_search mehtod which is call instead, and secondly since both implementation return an instance of Search module the tub was really like this

    allow_any_instance_of(Sunspot::Rails::StubSessionProxy::Search).to receive(:results).and_return(myExpectedResults)

    Read more https://github.com/sunspot/sunspot/blob/964ecd0ed0a0c949f399d93cba42021c92cf6062/sunspot_rails/lib/sunspot/rails/stub_session_proxy.rb