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

Recommended Webmock integration is incorrect #100

Closed
paneq opened this issue Nov 2, 2016 · 5 comments
Closed

Recommended Webmock integration is incorrect #100

paneq opened this issue Nov 2, 2016 · 5 comments

Comments

@paneq
Copy link

paneq commented Nov 2, 2016

Hey,

I think the recommended webmock setup in https://github.com/algolia/algoliasearch-client-ruby#mocking does not work in case of multiple test files because require will only load the code once. I think the stubs needs to be loaded in every test. Here is what I am doing instead:

RSpec.describe Event do

  specify do
    load $LOADED_FEATURES.find{|x| x.include?("algolia/version.rb") }.gsub("version.rb", "webmock.rb")

    # code which triggers algolia integration goes here....

    expect(WebMock).to have_requested(:put, "https://application_id.algolia.net/1/indexes/events/#{event.id}").
      with(body: hash_including({
        "objectID": event.id.to_s,
        "name": "dummy event",
        "category": "music",
      }))
  end

end

The key difference is that I do load instead of require. Unfortunately doing load 'algolia/webmock' did not just work so I had to use $LOADED_FEATURES.find{|x| x.include?("algolia/version.rb") }.gsub("version.rb", "webmock.rb") to find the file and load it.

I need to do it in every affected test.

@redox
Copy link

redox commented Nov 2, 2016

I think the stubs needs to be loaded in every test

Hmmm, maybe that's because you disable the mocking in a helper file or something required in every single test file?

In such case, the require should be done there as well.

The key difference is that I do load instead of require. Unfortunately doing load 'algolia/webmock' did not just work so I had to use $LOADED_FEATURES.find{|x| x.include?("algolia/version.rb") }.gsub("version.rb", "webmock.rb") to find the file and load it.

Yes, not very convenient :/

@paneq
Copy link
Author

paneq commented Nov 6, 2016

In such case, the require should be done there as well.

How would that help if the require is only executed once and that's the point of using require over load ?

As to our setup, rails_helper has:

  config.before(:each) do |example|
    WebMock.disable_net_connect!(allow_localhost: true, allow: "localhost:9200")
  end

  config.after(:each) do |example|
    WebMock.allow_net_connect!
  end

@redox
Copy link

redox commented Nov 7, 2016

So should we wrap all the stubs in a method you would call in your config.before to setup everything?

Something like: Algolia::WebMock.enable! ?

@paneq
Copy link
Author

paneq commented Nov 8, 2016

@redox sounds like a good approach.

@mintuhouse
Copy link

I am facing a similar issue. What is a recommended way to mock all algolia requests across the test suite?

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

No branches or pull requests

3 participants