public
Description: PLEASE CHECK http://github.com/lifo/docrails/wikis
Homepage: http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch
Clone URL: git://github.com/lifo/docrails.git
Added documentation for ActiveResource::HttpMock.

Special thanks to Clever Title Pending:
  http://seangeo.blogspot.com/2007/09/testing-activeresource.html
Squeegy (author)
Wed May 21 08:10:47 -0700 2008
commit  028a236f102cb617d1c3e39d3de9d51b99c4fd61
tree    c4e02aae0f6965ea3d61ee901059fa02f8456d80
parent  d79becdf21fd85432eceead107c37ccb5dea33c1
...
2
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
 
8
9
10
...
19
20
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
23
24
25
 
 
 
 
26
27
28
29
30
 
 
 
 
31
32
33
...
39
40
41
42
 
 
43
44
45
...
65
66
67
68
69
 
 
70
71
72
...
135
136
137
138
 
139
140
141
...
2
3
4
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
51
52
53
54
...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
90
91
92
93
94
95
96
 
 
97
98
99
100
101
102
103
...
109
110
111
 
112
113
114
115
116
...
136
137
138
 
 
139
140
141
142
143
...
206
207
208
 
209
210
211
212
0
@@ -2,9 +2,53 @@ require 'active_resource/connection'
0
 
0
 module ActiveResource
0
   class InvalidRequestError < StandardError; end #:nodoc:
0
-
0
+  
0
+  # One thing that has always been a pain with remote web services is testing.  The <tt>HttpMock</tt>
0
+  # class makes it easy to test your Active Resource models by creating a set of mock responses to specific
0
+  # requests.
0
+  #
0
+  # To test your Active Resource model, you simply call the <tt>ActiveResource::HttpMock.respond_to</tt>
0
+  # method with an attached block.  The block declares a set of URIs with expected input, and the output
0
+  # each request should return.  The passed in block has any number of entries in the following generalized
0
+  # format:
0
+  #
0
+  #   mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
0
+  #
0
+  # * <tt>http_method</tt> - The HTTP method to listen for.  This can be +get+, +post+, +put+, +delete+ or
0
+  #   +head+.
0
+  # * <tt>path</tt> - A string, starting with a <tt>"/"</tt>, defining the URI that is expected to be
0
+  #   called.
0
+  # * <tt>request_headers</tt> - Headers that are expected along with the request.  This argument uses a
0
+  #   hash format, such as <tt>{ "Content-Type" => "application/xml" }</tt>.  This mock will only trigger
0
+  #   if your tests sends a request with identical headers.
0
+  # * <tt>body</tt> - The data to be returned.  This should be a string of ActiveResource parseable content,
0
+  #   such as XML.
0
+  # * <tt>status</tt> - The HTTP response code, as an integer, to return with the response.
0
+  # * <tt>response_headers</tt> - Headers to be returned with the response.  Uses the same hash format as
0
+  #   <tt>request_headers</tt> listed above.
0
+  #
0
+  # In order for a mock to deliver its content, the incoming request must match by the <tt>http_method</tt>,
0
+  # +path+ and <tt>request_headers</tt>.  If no match is found  an <tt>InvalidRequestError</tt> exception
0
+  # will be raised letting you know you need to create a new mock for that request.
0
+  #
0
+  # ==== Example
0
+  #   def setup
0
+  #     @matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
0
+  #     ActiveResource::HttpMock.respond_to do |mock|
0
+  #       mock.post   "/people.xml",   {}, @matz, 201, "Location" => "/people/1.xml"
0
+  #       mock.get    "/people/1.xml", {}, @matz
0
+  #       mock.put    "/people/1.xml", {}, nil, 204
0
+  #       mock.delete "/people/1.xml", {}, nil, 200
0
+  #     end
0
+  #   end
0
+  #   
0
+  #   def test_get_matz
0
+  #     person = Person.find(1)
0
+  #     assert_equal "Matz", person.name
0
+  #   end
0
+  #
0
   class HttpMock
0
-    class Responder
0
+    class Responder #:nodoc:
0
       def initialize(responses)
0
         @responses = responses
0
       end
0
@@ -19,15 +63,41 @@ module ActiveResource
0
     end
0
 
0
     class << self
0
+      
0
+      # Returns an array of all request objects that have been sent to the mock.  You can use this to check
0
+      # wether or not your model actually sent an HTTP request.
0
+      #
0
+      # ==== Example
0
+      #   def setup
0
+      #     @matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
0
+      #     ActiveResource::HttpMock.respond_to do |mock|
0
+      #       mock.get "/people/1.xml", {}, @matz
0
+      #     end
0
+      #   end
0
+      #   
0
+      #   def test_should_request_remote_service
0
+      #     person = Person.find(1)  # Call the remote service
0
+      #     
0
+      #     # This request object has the same HTTP method and path as declared by the mock
0
+      #     expected_request = ActiveResource::Request.new(:get, "/people/1.xml")
0
+      #     
0
+      #     # Assert that the mock received, and responded to, the expected request from the model
0
+      #     assert ActiveResource::HttpMock.requests.include?(expected_request)
0
+      #   end
0
       def requests
0
         @@requests ||= []
0
       end
0
-
0
+      
0
+      # Returns a hash of <tt>request => response</tt> pairs for all all responses this mock has delivered, where +request+
0
+      # is an instance of <tt>ActiveResource::Request</tt> and the response is, naturally, an instance of
0
+      # <tt>ActiveResource::Response</tt>.
0
       def responses
0
         @@responses ||= {}
0
       end
0
-
0
-      def respond_to(pairs = {})
0
+      
0
+      # Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main
0
+      # <tt>ActiveResource::HttpMock</tt> description for a more detailed explanation.
0
+      def respond_to(pairs = {}) #:yields: mock
0
         reset!
0
         pairs.each do |(path, response)|
0
           responses[path] = response
0
@@ -39,7 +109,8 @@ module ActiveResource
0
           Responder.new(responses)
0
         end
0
       end
0
-
0
+      
0
+      # Deletes all logged requests and responses.
0
       def reset!
0
         requests.clear
0
         responses.clear
0
@@ -65,8 +136,8 @@ module ActiveResource
0
         end
0
       EOE
0
     end
0
-
0
-    def initialize(site)
0
+    
0
+    def initialize(site) #:nodoc:
0
       @site = site
0
     end
0
   end
0
@@ -135,7 +206,7 @@ module ActiveResource
0
     end
0
   end
0
 
0
-  class Connection
0
+  class Connection 
0
     private
0
       silence_warnings do
0
         def http

Comments