Open exocode opened 3 years ago
@exocode Having the same issue. Seems like my current setup doesn't work properly, there are inconsitencies. I couldn't find any documentation on integrating with rspec. Can somebody please suggest something?
just wanna keep that issue open
I also have the same issue... it seems like there is a way to use refresh:wait_for
but I am not sure how exactly to use it.
I was able to get a simple and reliable setup by:
Adding an initialize to force the use of the refresh setting in the test environment, as I described here.
Adding the following hook to my rails_helper.rb
:
RSpec.configure do |config|
config.before do |example|
next unless example.metadata[:elasticsearch]
Elasticsearch::Model.client.cat.indices(format: "json").pluck("index").each do |name|
Elasticsearch::Model.client.indices.delete(index: name)
end
Elasticsearch::Model::Registry.all.each do |model_klass|
model_klass.__elasticsearch__.create_index!
end
end
end
With this setup, any example tagged with :elasticsearch
, e.g.:
it "does something", :elasticsearch do
# ...
end
will start with a fresh set of Elasticsearch indices created based on your model register, which largely mimics the standard behaviour with the test database, where changes are rolled back after every example to ensure that there are no dependencies.
This setup allows me to write complex tests with records instantiated from multiple searchable models without the need to call any API methods to create, refresh or delete indices on invididual model classes.
@alexander-makarenko After adding your code I got WebMock::NetConnectNotAllowedError
.
So right after if Rails.env.test?
inside config/initializers/elasticsearch.rb
I added this piece.
require "webmock"
WebMock.disable_net_connect!(allow_localhost: true)
Works now. Thanks!
Hi there,
Can someone help me and confirm my approach or provide an actual (version 7.x) example how to setup a
rSpec
-environment forelasticsearch-rails (7.1.1)
correctly? I am usingRails 6.1.x
andrSpec 3.1.x
.I have doubt if my request tests are setup correctly, or if there is a better approach:
I saw examples with a
Product.import
andsleep 2
in specs, as well some real complex methods in the spec_helper.rb/rails_helper.rb file.Currently I have this, it looks slim and works (till now):
rails_helper.rb
product_request_spec.rb
Other approaches product_request_spec.rb
approach a.) (does not work always)
approach b.) Works but very slow if test amount raise
(maybe the docs can supplied with such an example... )
Thank you in advance