Skip to content
This repository
Browse code

Adding option to ActiveResource to allow you to not reset the previou…

…sly stored requests and responses by passing false to respond_to

Backport of commit 2a1b23f on rails/master
  • Loading branch information...
commit bac12fa5fc0bb25c7caab47451bffb6ec48c8bf3 1 parent 56fdfeb
Mikel Lindsaar authored jeremy committed
40  activeresource/lib/active_resource/http_mock.rb
@@ -133,8 +133,44 @@ def responses
133 133
       #   pairs = {create_matz => created_response, get_matz => ok_response}
134 134
       # 
135 135
       #   ActiveResource::HttpMock.respond_to(pairs)
136  
-      def respond_to(pairs = {}) #:yields: mock
137  
-        reset!
  136
+      #
  137
+      # Note, by default, every time you call +respond_to+, any previous request and response pairs stored
  138
+      # in HttpMock will be deleted giving you a clean slate to work on.
  139
+      # 
  140
+      # If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+
  141
+      # 
  142
+      # === Example
  143
+      # 
  144
+      #   ActiveResource::HttpMock.respond_to do |mock|
  145
+      #     mock.send(:get, "/people/1", {}, "XML1")
  146
+      #   end
  147
+      #   ActiveResource::HttpMock.responses.length #=> 1
  148
+      #   
  149
+      #   ActiveResource::HttpMock.respond_to(false) do |mock|
  150
+      #     mock.send(:get, "/people/2", {}, "XML2")
  151
+      #   end
  152
+      #   ActiveResource::HttpMock.responses.length #=> 2
  153
+      # 
  154
+      # This also works with passing in generated pairs of requests and responses, again, just pass in false
  155
+      # as the last argument:
  156
+      # 
  157
+      # === Example
  158
+      # 
  159
+      #   ActiveResource::HttpMock.respond_to do |mock|
  160
+      #     mock.send(:get, "/people/1", {}, "XML1")
  161
+      #   end
  162
+      #   ActiveResource::HttpMock.responses.length #=> 1
  163
+      # 
  164
+      #   get_matz         = ActiveResource::Request.new(:get, '/people/1.xml', nil)
  165
+      #   ok_response      = ActiveResource::Response.new("", 200, {})
  166
+      # 
  167
+      #   pairs = {get_matz => ok_response}
  168
+      #
  169
+      #   ActiveResource::HttpMock.respond_to(pairs, false)
  170
+      #   ActiveResource::HttpMock.responses.length #=> 2
  171
+      def respond_to(*args) #:yields: mock
  172
+        pairs = args.first || {}
  173
+        reset! if args.last.class != FalseClass
138 174
         responses.concat pairs.to_a
139 175
         if block_given?
140 176
           yield Responder.new(responses)
26  activeresource/test/http_mock_test.rb
@@ -119,6 +119,32 @@ def setup
119 119
     assert_equal 1, ActiveResource::HttpMock.responses.length
120 120
   end
121 121
 
  122
+  test "allows you to add new responses to the existing responses by calling a block" do
  123
+    ActiveResource::HttpMock.respond_to do |mock|
  124
+      mock.send(:get, "/people/1", {}, "XML1")
  125
+    end
  126
+    assert_equal 1, ActiveResource::HttpMock.responses.length
  127
+
  128
+    ActiveResource::HttpMock.respond_to(false) do |mock|
  129
+      mock.send(:get, "/people/2", {}, "XML2")
  130
+    end
  131
+    assert_equal 2, ActiveResource::HttpMock.responses.length
  132
+  end
  133
+
  134
+  test "allows you to add new responses to the existing responses by passing pairs" do
  135
+    ActiveResource::HttpMock.respond_to do |mock|
  136
+      mock.send(:get, "/people/1", {}, "XML1")
  137
+    end
  138
+    assert_equal 1, ActiveResource::HttpMock.responses.length
  139
+
  140
+    matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
  141
+    get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
  142
+    ok_response = ActiveResource::Response.new(matz, 200, {})
  143
+    ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
  144
+
  145
+    assert_equal 2, ActiveResource::HttpMock.responses.length
  146
+  end
  147
+
122 148
   def request(method, path, headers = {}, body = nil)
123 149
     if [:put, :post].include? method
124 150
       @http.send(method, path, body, headers)

0 notes on commit bac12fa

Please sign in to comment.
Something went wrong with that request. Please try again.