[Bug] Stub is still counted even after being removed
Test case
require 'test_helper'
class FooTest < ActiveSupport::TestCase
test "foo" do
google = "https://www.google.com"
google_stub1 = stub_request(:get, google)
Typhoeus.get(google)
assert_requested google_stub1
remove_request_stub google_stub1
google_stub2 = stub_request(:get, google)
Typhoeus.get(google)
assert_requested google_stub2
remove_request_stub google_stub2
end
end
Expected behaviour
Test should pass.
Since the stubbed request have been removed, both stub and number of times it have been requested, should not be carried over and should not affect any other stubs. When I stub the request, make the request, assert the stub, and then remove the stub, I expect the next stub with same url to be a properly isolated object that does not know, and does not care, about anything that happened before, after, or during it's existence.
Instead, we observe a breach of isolation:
Actual behaviour
FAIL FooTest#test_foo (0.20s)
The request GET https://www.google.com/ was expected to execute 1 time but it executed 2 times
The following requests were made:
GET https://www.google.com/ with headers {'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'} was made 2 times
============================================================
test/foo_test.rb:14:in `block in <class:FooTest>'
UPD.
#548 seems to be related.
I guess the problem is assert_requested is checking not the stub object, as one would expect from the syntax, but the global registry of requests instead.
support for passing a stub to assert_requested has been added after assert_requested was originally created to make it easier and avoid duplication, but the assertion is made on request registry and not on the stub itself.
stubs do not track their history of requests.
stubs do not track their history of requests.
Well yes, that's what this whole issue is about. They are expected to.
According to the semantics and signature of stub_request, assert_requested <stub>, and remove_request_stub methods, one would expect them to be idempotent.
remove_request_stub is expected to actually remove the stub and all the information related to it.
Which is not happening.
It would be really great if those methods could work as described (either by simply removing entries from registry, altering the request count, setting some flags, counters, or redesigning the whole registry approach in a different manner). Or at least, please, have it well documented, both in the methods descriptions and the readme. That alone could save quite a number of wasted brain cells of fellow developers out there. ;)
"If it's not documented, it's not a feature, it's a bug."
Thanks for looking into it! 👍