Skip to content

Instantly share code, notes, and snippets.

@nalanj
Created March 10, 2009 02:10
Show Gist options
  • Select an option

  • Save nalanj/76673 to your computer and use it in GitHub Desktop.

Select an option

Save nalanj/76673 to your computer and use it in GitHub Desktop.
# Reusable mocking of ActiveResource
#
# I use this technique when I need to mock ActiveResource in several tests and it gets a bit too keyboard
# heavy to use the default mechanism.
# Shortcut for typing ActiveResource::HttpMock.respond_to
def ar_mock(&block)
ActiveResource::HttpMock.respond_to(&block)
end
module ActiveResource
class HttpMock
class Responder
# Mocks create and get for a remote user object.
def user(user_hash)
post "/users.xml", request_headers,
user_hash.to_xml(:root => "user"), 200, {
'Location' => "http://0.0.0.0:3000/users/#{user_hash[:id]}"
}
get "/users/#{id}.xml", request_headers,
user_hash[:id]
end
end
end
end
# Actually using it:
ar_mock do |mock|
mock.user({ :username => "alan", :password => "supersecret", :id => 1 })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment