aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-08-30 15:35:36 +1000
committerJeremy Kemper <jeremy@bitsweat.net>2010-08-29 22:54:19 -0700
commit58d0e2c23c0c5578f23a4f4c639db5c0009b80c2 (patch)
treea8b5477b66ffa9afb51f8a315bd2d1e8656202ac /activeresource
parenta2996422935e46f68087b6bb11f70919b086c35f (diff)
downloadrails-58d0e2c23c0c5578f23a4f4c639db5c0009b80c2.tar.gz
rails-58d0e2c23c0c5578f23a4f4c639db5c0009b80c2.tar.bz2
rails-58d0e2c23c0c5578f23a4f4c639db5c0009b80c2.zip
Adding option to ActiveResource to allow you to not reset the previously stored requests and responses by passing false to respond_to
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/lib/active_resource/http_mock.rb40
-rw-r--r--activeresource/test/cases/http_mock_test.rb42
2 files changed, 79 insertions, 3 deletions
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)
diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb
index be0568b1a9..d90d1e01b8 100644
--- a/activeresource/test/cases/http_mock_test.rb
+++ b/activeresource/test/cases/http_mock_test.rb
@@ -88,7 +88,7 @@ class HttpMockTest < ActiveSupport::TestCase
assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body
end
- test "resets all mocked responses on each call to respond_to by default" do
+ test "resets all mocked responses on each call to respond_to with a block by default" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
end
@@ -100,6 +100,46 @@ class HttpMockTest < ActiveSupport::TestCase
assert_equal 1, ActiveResource::HttpMock.responses.length
end
+ test "resets all mocked responses on each call to respond_to by passing pairs by default" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
+ get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
+ ok_response = ActiveResource::Response.new(matz, 200, {})
+ ActiveResource::HttpMock.respond_to({get_matz => ok_response})
+
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to add new responses to the existing responses by calling a block" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ ActiveResource::HttpMock.respond_to(false) do |mock|
+ mock.send(:get, "/people/2", {}, "XML2")
+ end
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to add new responses to the existing responses by passing pairs" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
+ get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
+ ok_response = ActiveResource::Response.new(matz, 200, {})
+ ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
+
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ end
+
def request(method, path, headers = {}, body = nil)
if [:put, :post].include? method
@http.send(method, path, body, headers)