webmock
webmock copied to clipboard
webmock must be fully enabled after disabling for disable_net_connect to work
The following spec file demonstrates that disable!
followed by disable_net_connect!
doesn't work as expected. You have to enable!
after disable!
in order to then disable_net_connect!
:
require 'rails_helper'
RSpec.describe 'webmock' do
context 'shouldnt work' do
it 'webmock enabled' do # fails
WebMock.enable!
Excon.get('https://google.com')
end
it 'webmock disable net connect' do #fails
WebMock.disable_net_connect!
Excon.get('https://google.com')
end
it 'disable net after disable' do #passes!!!
WebMock.disable!
WebMock.disable_net_connect!
Excon.get('https://google.com')
end
it 'disable after enable' do #fails
WebMock.disable!
WebMock.enable!
WebMock.disable_net_connect!
Excon.get('https://google.com')
end
end
end
I'm not sure if this is "as intended" or is a bug.
@thoraxe yes, this is intended. when WebMock is disabled, disable_net_connect doesn't have any effect on WebMock since WebMock doesn't do anything.
@bblimke it's not clear to me from the documentation that you need to re-enable everything to then disable net connections. I'd be willing to attempt a docs PR if you'd consider it.
@thoraxe enabling/disabling WebMock and enabling/disabling net connections are orthogonal concerns.
enable!
and disable!
make WebMock to start interacting or stop interacting with http client adapters.
disable_net_connect!
is a configuration which tells WebMock whether to allow real net connections to occur or whether the requests should be blocked by WebMock unless they are allowed or stubbed.
WebMock is not only used to stub but also to track the real requests. In that case WebMock needs to be enabled to track the requests but net connections are not disabled.
If WebMock is disabled, then it has no way of neither intercepting nor tracking the requests, therefore disable_net_connect
has no effect as no requests go through WebMock.
I'll be very happy to merge a PR if you can improve the documentation to clarify that. Thank you.