From c1b49f1e18e08580196f5acfaacebcf4c3aa17d3 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Wed, 25 Aug 2010 12:05:23 +1000 Subject: Make ActiveResource::InvalidRequestError more user friendly Signed-off-by: Xavier Noria --- activeresource/lib/active_resource/http_mock.rb | 8 ++++++-- activeresource/test/cases/http_mock_test.rb | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index a98af88a37..8753a21835 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -126,7 +126,7 @@ module ActiveResource # if response = self.class.responses.assoc(request) # response[1] # else - # raise InvalidRequestError.new("No response recorded for #{request}") + # raise InvalidRequestError.new("Could not find a response recorded for #{request.to_s} - Responses recorded are: - #{inspect_responses}") # end # end module_eval <<-EOE, __FILE__, __LINE__ + 1 @@ -136,7 +136,7 @@ module ActiveResource if response = self.class.responses.assoc(request) response[1] else - raise InvalidRequestError.new("No response recorded for \#{request}") + raise InvalidRequestError.new("Could not find a response recorded for \#{request.to_s} - Responses recorded are: \#{inspect_responses}") end end EOE @@ -146,6 +146,10 @@ module ActiveResource def initialize(site) #:nodoc: @site = site end + + def inspect_responses #:nodoc: + self.class.responses.map { |r| r[0].to_s }.inspect + end end class Request diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb index 5e032d03f1..a387cd20b1 100644 --- a/activeresource/test/cases/http_mock_test.rb +++ b/activeresource/test/cases/http_mock_test.rb @@ -59,6 +59,17 @@ class HttpMockTest < ActiveSupport::TestCase assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body end + + test "raises InvalidRequestError if no response found for the #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML") + end + + assert_raise(::ActiveResource::InvalidRequestError) do + request(method, "/people/1", FORMAT_HEADER[method] => "application/json") + end + end + end def request(method, path, headers = {}, body = nil) -- cgit v1.2.3 From 5430f5bd06ba71a11479bc83eda38e0d7302ecea Mon Sep 17 00:00:00 2001 From: Jaime Iniesta Date: Thu, 26 Aug 2010 09:55:26 +0200 Subject: lifecycle should be two words, life cycle --- activeresource/README.rdoc | 2 +- activeresource/lib/active_resource/base.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activeresource') diff --git a/activeresource/README.rdoc b/activeresource/README.rdoc index 02398d969d..0aaad1d097 100644 --- a/activeresource/README.rdoc +++ b/activeresource/README.rdoc @@ -30,7 +30,7 @@ that inherits from ActiveResource::Base and providing a site class vari end Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes -lifecycle methods that operate against a persistent store. +life cycle methods that operate against a persistent store. # Find a person with id = 1 ryan = Person.find(1) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index a462f70684..7963aa462f 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -35,7 +35,7 @@ module ActiveResource # end # # Now the Person class is mapped to RESTful resources located at http://api.people.com:3000/people/, and - # you can now use Active Resource's lifecycle methods to manipulate resources. In the case where you already have + # you can now use Active Resource's life cycle methods to manipulate resources. In the case where you already have # an existing model with the same name as the desired RESTful resource you can set the +element_name+ value. # # class PersonResource < ActiveResource::Base @@ -51,7 +51,7 @@ module ActiveResource # end # # - # == Lifecycle methods + # == Life cycle methods # # Active Resource exposes methods for creating, finding, updating, and deleting resources # from REST web services. @@ -70,12 +70,12 @@ module ActiveResource # # ryan.destroy # => true # - # As you can see, these are very similar to Active Record's lifecycle methods for database records. + # As you can see, these are very similar to Active Record's life cycle methods for database records. # You can read more about each of these methods in their respective documentation. # # === Custom REST methods # - # Since simple CRUD/lifecycle methods can't accomplish every task, Active Resource also supports + # Since simple CRUD/life cycle methods can't accomplish every task, Active Resource also supports # defining your own custom REST methods. To invoke them, Active Resource provides the get, # post, put and \delete methods where you can specify a custom REST method # name to invoke. -- cgit v1.2.3