Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct setup for rSpec #988

Open
exocode opened this issue Apr 2, 2021 · 3 comments
Open

Correct setup for rSpec #988

exocode opened this issue Apr 2, 2021 · 3 comments
Labels

Comments

@exocode
Copy link

exocode commented Apr 2, 2021

Hi there,

Can someone help me and confirm my approach or provide an actual (version 7.x) example how to setup a rSpec-environment for elasticsearch-rails (7.1.1) correctly? I am using Rails 6.1.x and rSpec 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 and sleep 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

  # pass 'elasticsearch: true' or ':elasticsearch' to your tests to run this code
  config.around(:each, elasticsearch: true) do |example|
    Product.__elasticsearch__.create_index!(force: true)
    Product.import
    Product.__elasticsearch__.refresh_index!
    example.run
    Product.__elasticsearch__.client.indices.delete index: Product.index_name
  end

  config.after(:suite) do
    Elasticsearch::Model.client.indices.delete(index: '_all')
  end

product_request_spec.rb

  describe "by_name" do
    before(:each) do
      target = Fabricate :product, { name: term }
      Fabricate.times(3, :product)
      # Product.import(force: true) # sometimes it looks I have to use that
    end

    it 'respond with a pagination' do
      post "/search/by_name/#{term}"
      expect(response.body).to have_json_path("data")
    end
  end

Other approaches
product_request_spec.rb

approach a.) (does not work always)

before(:each) do
  Product.__elasticsearch__.create_index! force: true
  Fabricate :product
  Product.__elasticsearch__.client.indices.refresh
end

approach b.) Works but very slow if test amount raise

before(:each) do
  Product.__elasticsearch__.create_index! force: true
  Fabricate :product
  Product.import(force: true)
  sleep 2 # but slowing down hundred of tests is not my favorite.
end

(maybe the docs can supplied with such an example... )

Thank you in advance

@AndreiMotinga
Copy link

@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?

@stale stale bot added the stale label Jan 9, 2022
@exocode
Copy link
Author

exocode commented Jan 9, 2022

just wanna keep that issue open

@stale stale bot removed the stale label Jan 9, 2022
@elastic elastic deleted a comment from stale bot Jan 10, 2022
@ritec
Copy link

ritec commented Feb 24, 2023

I also have the same issue... it seems like there is a way to use refresh:wait_forbut I am not sure how exactly to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants