ankane / searchkick

Intelligent search made easy
MIT License
6.52k stars 754 forks source link

Searchkick not returning any results in RSpec test #1166

Closed DaniG2k closed 6 years ago

DaniG2k commented 6 years ago

I'm trying to test a class with RSpec which makes use of Searchkick.

I have the following in my spec support directory:

RSpec.configure do |config|
  config.before(:suite) do
    # Reindex models
    Restaurant.reindex
    # and disable callbacks
    Searchkick.disable_callbacks
  end

  config.around(:each, search: true) do |example|
    Searchkick.callbacks(true) do
      example.run
    end
  end
end

and my test looks like this:

describe Restaurants::SearchFacade, search: true do
  subject { described_class }

  describe '#search' do
    context 'with a query' do
      let!(:restaurant_1) { create(:restaurant, description: 'Amazing sour dough pizza place') }
      let!(:restaurant_2) { create(:restaurant) }

      it 'returns values containing the query' do
        Restaurant.search_index.refresh
        params = { search: { query: 'sour dough' } }
        facade = subject.new(params)

        expect(facade.search.total_count).to eq(1)
      end
    end
  end
end

I should be receiving 1 result. However, Searchkick returns 0 results. This is not a problem with the facade since I've also tried with Restaurant.search('*') which should return all results but nothing is being returned.

My Restaurant class looks like this:

class Restaurant < ApplicationRecord
  searchkick searchable: %i[title description street_address]
end

Any idea what may be causing this? Any help would be much appreciated. Thanks in advance!

ankane commented 6 years ago

Hey @DaniG2k, please use Stack Overflow for questions. If you use Factory Bot, be sure to follow those instructions as well.

DaniG2k commented 6 years ago

Thanks for your response. I've asked on SO already and am using the FactoryBot reindex trait but it still doesn't seem to work as intended.