mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
328ba3b333
Similar to Active Record - it will raise ActiveResouce::ResourceInvalid if the resource is not valid (ie if <tt>valid?</tt> returns false) However - does not raise ActiveResource::ResourceNotFound if the callbacks fail (callbacks have not yet been implemented) - it will just try to save and raise if the callbacks all fail. This is not ideal behaviour - but will do until we decide to change the behaviour of save_with_validations to actually raise (rather than catch) the ResourceInvalid exception. Signed-off-by: Joshua Peek <josh@joshpeek.com>
55 lines
1.9 KiB
Ruby
55 lines
1.9 KiB
Ruby
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
|
|
|