From 58d0e2c23c0c5578f23a4f4c639db5c0009b80c2 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Mon, 30 Aug 2010 15:35:36 +1000 Subject: Adding option to ActiveResource to allow you to not reset the previously stored requests and responses by passing false to respond_to --- activeresource/lib/active_resource/http_mock.rb | 40 +++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 2b75463454..ddd3fb1f5d 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -133,8 +133,44 @@ module ActiveResource # pairs = {create_matz => created_response, get_matz => ok_response} # # ActiveResource::HttpMock.respond_to(pairs) - def respond_to(pairs = {}) #:yields: mock - reset! + # + # Note, by default, every time you call +respond_to+, any previous request and response pairs stored + # in HttpMock will be deleted giving you a clean slate to work on. + # + # If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+ + # + # === Example + # + # ActiveResource::HttpMock.respond_to do |mock| + # mock.send(:get, "/people/1", {}, "XML1") + # end + # ActiveResource::HttpMock.responses.length #=> 1 + # + # ActiveResource::HttpMock.respond_to(false) do |mock| + # mock.send(:get, "/people/2", {}, "XML2") + # end + # ActiveResource::HttpMock.responses.length #=> 2 + # + # This also works with passing in generated pairs of requests and responses, again, just pass in false + # as the last argument: + # + # === Example + # + # ActiveResource::HttpMock.respond_to do |mock| + # mock.send(:get, "/people/1", {}, "XML1") + # end + # ActiveResource::HttpMock.responses.length #=> 1 + # + # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) + # ok_response = ActiveResource::Response.new("", 200, {}) + # + # pairs = {get_matz => ok_response} + # + # ActiveResource::HttpMock.respond_to(pairs, false) + # ActiveResource::HttpMock.responses.length #=> 2 + def respond_to(*args) #:yields: mock + pairs = args.first || {} + reset! if args.last.class != FalseClass responses.concat pairs.to_a if block_given? yield Responder.new(responses) -- cgit v1.2.3