aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/cases/validations_test.rb
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-08-19 11:09:24 -0300
committerEmilio Tagua <miloops@gmail.com>2009-08-19 11:09:24 -0300
commitb3427286771bb476c0c4a58488033bd671740332 (patch)
tree616c38e8ca5606e06c7f2fbfa7b732b38d2eaec4 /activeresource/test/cases/validations_test.rb
parent2048556a14c28f9814f9c6ad44a08084afec1afe (diff)
parent328ba3b333777bbc1269cbe0e9f590c845006c9d (diff)
downloadrails-b3427286771bb476c0c4a58488033bd671740332.tar.gz
rails-b3427286771bb476c0c4a58488033bd671740332.tar.bz2
rails-b3427286771bb476c0c4a58488033bd671740332.zip
Merge commit 'rails/master'
Diffstat (limited to 'activeresource/test/cases/validations_test.rb')
-rw-r--r--activeresource/test/cases/validations_test.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/activeresource/test/cases/validations_test.rb b/activeresource/test/cases/validations_test.rb
new file mode 100644
index 0000000000..a8ab7d64e7
--- /dev/null
+++ b/activeresource/test/cases/validations_test.rb
@@ -0,0 +1,55 @@
+require 'abstract_unit'
+require "fixtures/project"
+
+# The validations are tested thoroughly under ActiveModel::Validations
+# This test case simply makes sur that they are all accessible by
+# Active Resource objects.
+class ValidationsTest < ActiveModel::TestCase
+ VALID_PROJECT_HASH = { :name => "My Project", :description => "A project" }
+ def setup
+ @my_proj = VALID_PROJECT_HASH.to_xml(:root => "person")
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/projects.xml", {}, @my_proj, 201, 'Location' => '/projects/5.xml'
+ end
+ end
+
+ def test_validates_presence_of
+ p = new_project(:name => nil)
+ assert !p.valid?, "should not be a valid record without name"
+ assert !p.save, "should not have saved an invalid record"
+ assert_equal ["can't be blank"], p.errors[:name], "should have an error on name"
+
+ p.name = "something"
+
+ assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
+ end
+
+ def test_fails_save!
+ p = new_project(:name => nil)
+ assert_raise(ActiveResource::ResourceInvalid) { p.save! }
+ end
+
+
+ def test_validate_callback
+ # we have a callback ensuring the description is longer than three letters
+ p = new_project(:description => 'a')
+ assert !p.valid?, "should not be a valid record when it fails a validation callback"
+ assert !p.save, "should not have saved an invalid record"
+ assert_equal ["must be greater than three letters long"], p.errors[:description], "should be an error on description"
+
+ # should now allow this description
+ p.description = 'abcd'
+ assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
+ end
+
+ protected
+
+ # quickie helper to create a new project with all the required
+ # attributes.
+ # Pass in any params you specifically want to override
+ def new_project(opts = {})
+ Project.new(VALID_PROJECT_HASH.merge(opts))
+ end
+
+end
+