diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-21 23:31:21 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-21 23:31:21 +0000 |
commit | f99e5bba192938ed7e812f1ae82785ad796c636d (patch) | |
tree | 826e05f963c939db3d0640c87f8daeaebbbe4e43 | |
parent | 6dd10d85dab9d2623deb3dc4a61106ca9be1d981 (diff) | |
download | rails-f99e5bba192938ed7e812f1ae82785ad796c636d.tar.gz rails-f99e5bba192938ed7e812f1ae82785ad796c636d.tar.bz2 rails-f99e5bba192938ed7e812f1ae82785ad796c636d.zip |
Increase test coverage (closes #8699, #8700) [josh]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7532 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activeresource/test/base/custom_methods_test.rb | 26 | ||||
-rw-r--r-- | activeresource/test/base_errors_test.rb | 17 |
2 files changed, 32 insertions, 11 deletions
diff --git a/activeresource/test/base/custom_methods_test.rb b/activeresource/test/base/custom_methods_test.rb index 0e1c00c478..9864e9876f 100644 --- a/activeresource/test/base/custom_methods_test.rb +++ b/activeresource/test/base/custom_methods_test.rb @@ -18,12 +18,14 @@ class CustomMethodsTest < Test::Unit::TestCase mock.get "/people/1/deep.xml", {}, @matz_deep mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array mock.get "/people/managers.xml", {}, @matz_array + mock.post "/people/hire.xml?name=Matz", {}, nil, 201 mock.put "/people/1/promote.xml?position=Manager", {}, nil, 204 mock.put "/people/promote.xml?name=Matz", {}, nil, 204, {} mock.put "/people/sort.xml?by=name", {}, nil, 204 mock.delete "/people/deactivate.xml?name=Matz", {}, nil, 200 mock.delete "/people/1/deactivate.xml", {}, nil, 200 mock.post "/people/new/register.xml", {}, @ryan, 201, 'Location' => '/people/5.xml' + mock.post "/people/1/register.xml", {}, @matz, 201 mock.get "/people/1/addresses/1.xml", {}, @addy mock.get "/people/1/addresses/1/deep.xml", {}, @addy_deep mock.put "/people/1/addresses/1/normalize_phone.xml?locale=US", {}, nil, 204 @@ -35,23 +37,26 @@ class CustomMethodsTest < Test::Unit::TestCase def teardown ActiveResource::HttpMock.reset! end - + def test_custom_collection_method # GET assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz')) - + + # POST + assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz')) + # PUT assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:promote, {:name => 'Matz'}, 'atestbody') assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name') - + # DELETE Person.delete :deactivate, :name => 'Matz' - + # Nested resource assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name') end - + def test_custom_element_method # Test GET against an element URL assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'} @@ -69,20 +74,23 @@ class CustomMethodsTest < Test::Unit::TestCase assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US') end - + def test_custom_new_element_method # Test POST against a new element URL ryan = Person.new(:name => 'Ryan') assert_equal ActiveResource::Response.new(@ryan, 201, {'Location' => '/people/5.xml'}), ryan.post(:register) - + # Test POST against a nested collection URL addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1) assert_equal ActiveResource::Response.new({ :street => '12345 Street' }.to_xml(:root => 'address'), 201, {'Location' => '/people/1/addresses/2.xml'}), addy.post(:link) + + matz = Person.new(:id => 1, :name => 'Matz') + assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register) end - + def test_find_custom_resources assert_equal 'Matz', Person.find(:all, :from => :managers).first.name end -end
\ No newline at end of file +end diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 3527eb2353..f9aa58016d 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -9,22 +9,35 @@ class BaseErrorsTest < Test::Unit::TestCase @person = Person.new(:name => '', :age => '') assert_equal @person.save, false end - + def test_should_mark_as_invalid assert !@person.valid? end - + def test_should_parse_xml_errors assert_kind_of ActiveResource::Errors, @person.errors assert_equal 4, @person.errors.size end def test_should_parse_errors_to_individual_attributes + assert @person.errors.invalid?(:name) assert_equal "can't be blank", @person.errors.on(:age) assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] assert_equal "Person quota full for today.", @person.errors.on_base end + def test_should_iterate_over_errors + errors = [] + @person.errors.each { |attribute, message| errors << [attribute, message] } + assert_equal ["name", "can't be blank"], errors.first + end + + def test_should_iterate_over_full_errors + errors = [] + @person.errors.each_full { |message| errors << message } + assert_equal "Name can't be blank", errors.first + end + def test_should_format_full_errors full = @person.errors.full_messages assert full.include?("Age can't be blank") |