Project

esse-rspec

0.0
The project is in a healthy, maintained state
RSpec for Esse
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 1.20
~> 1.11, >= 1.11.5
~> 1.3

Runtime

>= 0.2.4
>= 3
 Project Readme

esse-rspec

RSpec and testing support for esse

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'esse-rspec'
end

And then execute:

$ bundle

Usage

Require the esse/rspec file in your spec_helper.rb file:

require 'esse/rspec'

Mock Requests

Stub a search request to specific index and return a response

expect(ProductsIndex).to esse_receive_request(:search)
  .with(body: {query: {match_all: {}}, size: 10})
  .and_return('hits' => { 'total' => 0, 'hits' => [] })

query = ProductsIndex.search(query: {match_all: {}}, size: 10)
query.response.total # => 0

Stub a search request to an index with a non 200 response

expect(ProductsIndex).to esse_receive_request(:search)
  .with(body: {query: {match_all: {}}, size: 10})
  .and_raise_http_status(500, {"error" => 'Something went wrong'})

begin
  ProductsIndex.search(query: {match_all: {}}, size: 10).response
rescue Esse::Transport::InternalServerError => e
  puts e.message # => {"error" => 'Something went wrong'}
end

Stub a cluster search request

expect(Esse.cluster(:default)).to esse_receive_request(:search)
  .with(index: 'geos_*', body: {query: {match_all: {}}, size: 10})
  .and_return('hits' => { 'total' => 0, 'hits' => [] })

query = Esse.cluster(:default).search('geos_*', body: {query: {match_all: {}}, size: 10})
query.response.total # => 0

Stub a api/transport request

expect(Esse.cluster).to esse_receive_request(:get)
  .with(id: '1', index: 'products')
  .and_return('_id' => '1', '_source' => {title: 'Product 1'})

Esse.cluster.api.get('1', index: 'products') # => { '_id' => '1', '_source' => {title: 'Product 1'} }

Stubbing Esse::Index classes

before do
  stub_esse_index('products') do
    repository :product, const: true do
      # ...
    end
  end
end

it 'defines the ProductsIndex class' do
  expect(ProductsIndex).to be < Esse::Index
  expect(ProductsIndex::Product).to be < Esse::Index::Repository
end