Skip to content
This repository
Browse code

Make ActiveResource::InvalidRequestError more user friendly

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
commit c1b49f1e18e08580196f5acfaacebcf4c3aa17d3 1 parent 1847d33
Mikel Lindsaar mikel authored fxn committed
8 activeresource/lib/active_resource/http_mock.rb
@@ -126,7 +126,7 @@ def reset!
126 126 # if response = self.class.responses.assoc(request)
127 127 # response[1]
128 128 # else
129   - # raise InvalidRequestError.new("No response recorded for #{request}")
  129 + # raise InvalidRequestError.new("Could not find a response recorded for #{request.to_s} - Responses recorded are: - #{inspect_responses}")
130 130 # end
131 131 # end
132 132 module_eval <<-EOE, __FILE__, __LINE__ + 1
@@ -136,7 +136,7 @@ def #{method}(path, #{'body, ' if has_body}headers)
136 136 if response = self.class.responses.assoc(request)
137 137 response[1]
138 138 else
139   - raise InvalidRequestError.new("No response recorded for \#{request}")
  139 + raise InvalidRequestError.new("Could not find a response recorded for \#{request.to_s} - Responses recorded are: \#{inspect_responses}")
140 140 end
141 141 end
142 142 EOE
@@ -146,6 +146,10 @@ def #{method}(path, #{'body, ' if has_body}headers)
146 146 def initialize(site) #:nodoc:
147 147 @site = site
148 148 end
  149 +
  150 + def inspect_responses #:nodoc:
  151 + self.class.responses.map { |r| r[0].to_s }.inspect
  152 + end
149 153 end
150 154
151 155 class Request
11 activeresource/test/cases/http_mock_test.rb
@@ -59,6 +59,17 @@ class HttpMockTest < ActiveSupport::TestCase
59 59 assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
60 60 assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
61 61 end
  62 +
  63 + test "raises InvalidRequestError if no response found for the #{method} request" do
  64 + ActiveResource::HttpMock.respond_to do |mock|
  65 + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
  66 + end
  67 +
  68 + assert_raise(::ActiveResource::InvalidRequestError) do
  69 + request(method, "/people/1", FORMAT_HEADER[method] => "application/json")
  70 + end
  71 + end
  72 +
62 73 end
63 74
64 75 def request(method, path, headers = {}, body = nil)

0 comments on commit c1b49f1

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