Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stub results? #1032

Open
cedvw opened this issue Jun 9, 2023 · 0 comments
Open

How to stub results? #1032

cedvw opened this issue Jun 9, 2023 · 0 comments

Comments

@cedvw
Copy link

cedvw commented Jun 9, 2023

I can't figure out how to stub sunspot results in RSpec.

I added require 'sunspot/rails/spec_helper' to my spec/rails_helper.rb file, and call disconnect_sunspot at the top of my test.

My understanding is that running allow_any_instance_of(Sunspot::Rails::StubSessionProxy::Search).to receive(:results).and_return(['the results']) should be all I need to do for a search query to return ['the results'], but no matter what I try, I always end up with an empty array ([]) as results.

Here is a simplified version of my controller:

@search_results = MyModel.search do
                    fulltext params[:search]
                  end.results

And my test (simplified) looks like this:

require 'rails_helper'

RSpec.describe 'My controller' do
  disconnect_sunspot

  before do
    allow_any_instance_of(Sunspot::Rails::StubSessionProxy::Search).to receive(:results).and_return(['the results'])

    # Code that calls the controller here
  end

  it 'returns what I want' do
    expect(response.body).to eq(['the results'])
  end
end

And the RSpec results:

Failure/Error: expect(response.body).to eq(['the results'])
     
       expected: ["the results"]
            got: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant