Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveResource Client Validation Tests #191

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion activeresource/test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ def test_validate_callback
p.description = 'abcd'
assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
end


def test_client_side_validation_maximum
project = Project.new(:description => '123456789012345')
assert ! project.valid?
assert_equal ['is too long (maximum is 10 characters)'], project.errors[:description]
end

protected

# quickie helper to create a new project with all the required
Expand Down
19 changes: 6 additions & 13 deletions activeresource/test/fixtures/project.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# used to test validations
class Project < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
schema do
string :email
string :name
end

validates_presence_of :name
validates :name, :presence => true
validates :description, :presence => false, :length => {:maximum => 10}
validate :description_greater_than_three_letters

# to test the validate *callback* works
def description_greater_than_three_letters
errors.add :description, 'must be greater than three letters long' if description.length < 3 unless description.blank?
end


# stop-gap accessor to default this attribute to nil
# Otherwise the validations fail saying that the method does not exist.
# In future, method_missing will be updated to not explode on a known
# attribute.
def name
attributes['name'] || nil
end
def description
attributes['description'] || nil
end
end