Skip to content

Instantly share code, notes, and snippets.

@khoan
Last active August 29, 2015 14:22
Show Gist options
  • Save khoan/53e9c62c3950ca8d41ad to your computer and use it in GitHub Desktop.
Save khoan/53e9c62c3950ca8d41ad to your computer and use it in GitHub Desktop.
make testing fast by stubbing out any call to elasticsearch cluster unless spec is tagged
# ruby 2.20
#
# gems
# rspec-3.2.0
# webmock-1.18.0
# elasticsearch-extensions-0.0.15
# elasticsearch-persistence-0.1.4
# Rake needs to be loaded for elasticsearch cluster
require 'rake'
# add some helper methods from lib/issue_data
load 'tasks/helper_methods.rake'
require 'elasticsearch/extensions/test/cluster/tasks'
require 'elasticsearch/persistence/model'
RSpec.configure do |config|
es_test_cluster = Elasticsearch::Extensions::Test::Cluster
es_test_cluster_running = false
config.before :context, elasticsearch: true do |context|
@elasticsearch_context = true
end
config.before :example do |example|
if @elasticsearch_context || example.metadata[:elasticsearch]
WebMock::Config.instance.allow_localhost = true
unless es_test_cluster.running?
es_test_cluster.start(nodes: 1)
es_test_cluster_running = true
end
purge_index Log, Item
else
stub_request(:any, /localhost:9250/).to_return(body: {hits: {total: 0}})
end
end
config.before :suite do
options = {host: 'localhost:9250'}
#options[:log] = true
Elasticsearch::Persistence.client = Elasticsearch::Client.new(options)
end
config.after :suite do
es_test_cluster.stop if es_test_cluster_running
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment