added a method to remove stubs #211

Closed
wants to merge 3 commits into
from
View
@@ -7,6 +7,11 @@ def stub_request(method, uri)
register_request_stub(WebMock::RequestStub.new(method, uri))
end
+ def remove_request_stub(method, uri)
+ WebMock::StubRegistry.instance.
+ unregister_request_stub(WebMock::RequestSignature.new(method, uri))
+ end
+
alias_method :stub_http_request, :stub_request
def a_request(method, uri)
@@ -31,6 +31,10 @@ def register_global_stub(&block)
global_stubs.push stub
end
+ def unregister_request_stub(signature)
+ self.request_stubs.delete_if { |stub| stub.matches?(signature) }
+ end
+
def register_request_stub(stub)
request_stubs.insert(0, stub)
stub
@@ -444,4 +444,36 @@ def stub_non_globally
end
end
end
+
+ describe "removing stubs" do
+
+ before(:each) do
+ stub_request(:get, "www.example.com").to_return(:body => "abc")
+ end
+
+ it "should be able to remove a previously stubbed uri" do
+ WebMock.remove_request_stub(:get, "www.example.com")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ end
+
+ it "should only remove the stub with the specified verb" do
+ stub_request(:post, "www.example.com")
+ WebMock.remove_request_stub(:get, "www.example.com")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ http_request(:post, "http://www.example.com/", :body => "abc").status.should == "200"
+ end
+
+ it "should only remove the stub with the specified uri" do
+ stub_request(:get, "www.another.example.com")
+ WebMock.remove_request_stub(:get, "www.example.com")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ http_request(:get, "http://www.another.example.com/").status.should == "200"
+ end
+ end
end
@@ -9,16 +9,28 @@
@request_stub = WebMock::RequestStub.new(:get, "www.example.com")
end
- describe "reset!" do
+ describe "cleaning the stubs" do
before(:each) do
WebMock::StubRegistry.instance.register_request_stub(@request_stub)
end
- it "should clean request stubs" do
+ it "should clean all request stubs" do
WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
WebMock::StubRegistry.instance.reset!
WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
end
+
+ it "should clean only one stub" do
+ another_request_signature = WebMock::RequestSignature.new(:get, "www.another.example.com")
+ another_request_stub = WebMock::RequestStub.new(:get, "www.another.example.com")
+ WebMock::StubRegistry.instance.register_request_stub(another_request_stub)
+
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
+ WebMock::StubRegistry.instance.unregister_request_stub(@request_signature)
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
+
+ WebMock::StubRegistry.instance.registered_request?(another_request_signature).should == another_request_stub
+ end
end
describe "registering and reporting registered requests" do