webmock icon indicating copy to clipboard operation
webmock copied to clipboard

webmock doesn't play nice with MiniTest hooks

Open kwasimensah opened this issue 8 years ago • 5 comments

I'm using webmocks to replace expensive calls to Facebook to create test users. I was previously using Minitest-hooks (https://github.com/jeremyevans/minitest-hooks) to do the creation in before_all and deletion in after_all blocks. However, webmock/minitest.rb monkey patches MiniTest's teardown causing code in after_all to fail.

I'm looking for the best way to manually control when webmock calls reset!.

kwasimensah avatar Sep 04 '16 14:09 kwasimensah

was able to get this working by adding this to the bottom of my rails project's test/test_helper.rb

ActiveSupport::TestCase.class_eval do
  alias_method :teardown, :teardown_without_webmock
  alias_method :after_all_without_webmock, :after_all

  def after_all_with_webmock
    after_all_without_webmock
    WebMock.reset!
  end

  alias_method :after_all, :after_all_with_webmock
end

kwasimensah avatar Sep 04 '16 16:09 kwasimensah

Hi @kwasimensah ,

Do you need further help on this or we can close this issue?

oliakremmyda avatar Feb 12 '18 18:02 oliakremmyda

Oh, haven't looked at this in a while. I believe my code still has the monkey patch. Are you asking if it works without that?

kwasimensah avatar Feb 12 '18 19:02 kwasimensah

@kwasimensah do you have any suggestions on how to change https://github.com/bblimke/webmock/blob/master/lib/webmock/minitest.rb in order to support minitest hooks?

bblimke avatar Feb 12 '18 21:02 bblimke

Facing this very same issue, with Rails 7, minitest 5.18.0 and minitest-hooks 1.5.0 and webmock 318.1

I guess the problem nowadays lies in the fact we run the tests in separate threads, so the "mocks" are not available in the running threads.

fidalgo avatar Apr 24 '23 19:04 fidalgo