Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure that global stubs take precedence over non-global stubs.

This is important for tools like VCR that use global stubs for more than just returning a stubbed response--for consistency, it needs to always have the stub block invoked so it can manage before/after request hooks.
  • Loading branch information...
commit a950ab839d3a83a21d284a18858fe1390a5d20d2 1 parent 6131be2
@myronmarston myronmarston authored
View
2  lib/webmock/stub_registry.rb
@@ -48,7 +48,7 @@ def response_for_request(request_signature)
private
def request_stub_for(request_signature)
- (request_stubs + global_stubs).detect { |registered_request_stub|
+ (global_stubs + request_stubs).detect { |registered_request_stub|
registered_request_stub.request_pattern.matches?(request_signature)
}
end
View
24 spec/acceptance/shared/stubbing_requests.rb
@@ -351,6 +351,30 @@
http_request(:get, "http://www.example.com/").body.should == "global stub body"
stub_invocation_order.should eq([:nil_stub, :hash_stub])
end
+
+ [:before, :after].each do |before_or_after|
+ context "when there is also a non-global registered stub #{before_or_after} the global stub" do
+ def stub_non_globally
+ stub_request(:get, "www.example.com").to_return(:body => 'non-global stub body')
+ end
+
+ define_method :register_stubs do |block|
+ stub_non_globally if before_or_after == :before
+ WebMock.globally_stub_request(&block)
+ stub_non_globally if before_or_after == :after
+ end
+
+ it 'uses the response from the global stub if the block returns a non-nil value' do
+ register_stubs(lambda { |req| { :body => 'global stub body' } })
+ http_request(:get, "http://www.example.com/").body.should == "global stub body"
+ end
+
+ it 'uses the response from the non-global stub if the block returns a nil value' do
+ register_stubs(lambda { |req| nil })
+ http_request(:get, "http://www.example.com/").body.should == "non-global stub body"
+ end
+ end
+ end
end
describe "when stubbing request with a block evaluated on request" do
Please sign in to comment.
Something went wrong with that request. Please try again.