Module: Chewy::Rspec::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/chewy/rspec/helpers.rb

Instance Method Summary collapse

Instance Method Details

#mock_elasticsearch_response(index, raw_response) ⇒ Object

Rspec helper to mock elasticsearch response To use it - add require 'chewy/rspec' to the spec_helper.rb

mock_elasticsearch_response(CitiesIndex, raw_response) expect(CitiesIndex.query({}).hits).to eq(hits)



11
12
13
14
15
# File 'lib/chewy/rspec/helpers.rb', line 11

def mock_elasticsearch_response(index, raw_response)
  mocked_request = Chewy::Search::Request.new(index)
  allow(Chewy::Search::Request).to receive(:new).and_return(mocked_request)
  allow(mocked_request).to receive(:perform).and_return(raw_response)
end

#mock_elasticsearch_response_sources(index, hits) ⇒ Object

Rspec helper to mock Elasticsearch response source To use it - add require 'chewy/rspec' to the spec_helper.rb

mock_elasticsearch_response_sources(CitiesIndex, sources) expect(CitiesIndex.query({}).hits).to eq(hits)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chewy/rspec/helpers.rb', line 23

def mock_elasticsearch_response_sources(index, hits)
  raw_response = {
    'took' => 4,
    'timed_out' => false,
    '_shards' => {
      'total' => 1,
      'successful' => 1,
      'skipped' => 0,
      'failed' => 0
    },
    'hits' => {
      'total' => {
        'value' => hits.count,
        'relation' => 'eq'
      },
      'max_score' => 1.0,
      'hits' => hits.each_with_index.map do |hit, i|
        {
          '_index' => index.index_name,
          '_type' => '_doc',
          '_id' => (i + 1).to_s,
          '_score' => 3.14,
          '_source' => hit
        }
      end
    }
  }

  mock_elasticsearch_response(index, raw_response)
end