Parent

Files

ActiveResource::HttpMock

One thing that has always been a pain with remote web services is testing. The HttpMock class makes it easy to test your Active Resource models by creating a set of mock responses to specific requests.

To test your Active Resource model, you simply call the ActiveResource::HttpMock.respond_to method with an attached block. The block declares a set of URIs with expected input, and the output each request should return. The passed in block has any number of entries in the following generalized format:

  mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})

In order for a mock to deliver its content, the incoming request must match by the http_method, path and request_headers. If no match is found an InvalidRequestError exception will be raised letting you know you need to create a new mock for that request.

Example

  def setup
    @matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
    ActiveResource::HttpMock.respond_to do |mock|
      mock.post   "/people.xml",   {}, @matz, 201, "Location" => "/people/1.xml"
      mock.get    "/people/1.xml", {}, @matz
      mock.put    "/people/1.xml", {}, nil, 204
      mock.delete "/people/1.xml", {}, nil, 200
    end
  end

  def test_get_matz
    person = Person.find(1)
    assert_equal "Matz", person.name
  end

Public Class Methods

requests() click to toggle source

Returns an array of all request objects that have been sent to the mock. You can use this to check if your model actually sent an HTTP request.

Example

  def setup
    @matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
    ActiveResource::HttpMock.respond_to do |mock|
      mock.get "/people/1.xml", {}, @matz
    end
  end

  def test_should_request_remote_service
    person = Person.find(1)  # Call the remote service

    # This request object has the same HTTP method and path as declared by the mock
    expected_request = ActiveResource::Request.new(:get, "/people/1.xml")

    # Assert that the mock received, and responded to, the expected request from the model
    assert ActiveResource::HttpMock.requests.include?(expected_request)
  end
    # File lib/active_resource/http_mock.rb, line 90
90:       def requests
91:         @@requests ||= []
92:       end
reset!() click to toggle source

Deletes all logged requests and responses.

     # File lib/active_resource/http_mock.rb, line 113
113:       def reset!
114:         requests.clear
115:         responses.clear
116:       end
respond_to(pairs = {}) click to toggle source

Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main ActiveResource::HttpMock description for a more detailed explanation.

     # File lib/active_resource/http_mock.rb, line 102
102:       def respond_to(pairs = {}) #:yields: mock
103:         reset!
104:         responses.concat pairs.to_a
105:         if block_given?
106:           yield Responder.new(responses)
107:         else
108:           Responder.new(responses)
109:         end
110:       end
responses() click to toggle source

Returns the list of requests and their mocked responses. Look up a response for a request using responses.assoc(request).

    # File lib/active_resource/http_mock.rb, line 96
96:       def responses
97:         @@responses ||= []
98:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.