I was suffered at the begining of this testing but later I've found some sample ways to solve it. //Testing with mocking object's method
//Sample structure of HttpMock: mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
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
------------------------------------
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
------------------------------------
def test_delete
assert Person.delete(1)
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 404
end
assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) }
end
------------------------------------
@matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, @matz
end
//Testing by deleting the record and return RecordNotFound(404)
def test_delete
assert Person.delete(1)
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 404
end
assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) }
end
October 08, 2009
Testing ActiveResource
Posted by samneang
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment