From 0f48036de78b00c85367bdfb023b95110d274857 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 19 Dec 2007 18:23:04 +0000 Subject: [PATCH] ActionItem22: making tests pass --- app/models/comment.rb | 13 +++++++++++++ test/test_helper.rb | 6 ++++++ test/unit/comment_test.rb | 24 +++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index a34b704..2bccb89 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,4 +2,17 @@ class Comment < ActiveRecord::Base validates_presence_of :title, :body belongs_to :article belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' + + # unauthenticated authors: + validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) + validates_presence_of :email, :if => (lambda { |record| !record.name.blank? }) + + # require either a recognized author or an external person + validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? }) + validates_each :name do |rec,attribute,value| + if rec.author_id && (!rec.name.blank? || !rec.email.blank?) + rec.errors.add(:name, _('%{fn} can only be informed for unauthenticated authors')) + end + end + end diff --git a/test/test_helper.rb b/test/test_helper.rb index bdb7d33..08329e2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -98,6 +98,12 @@ class Test::Unit::TestCase assert !object.errors.invalid?(attribute), "Attribute \"#{attribute.to_s}\" expected to accept value #{test_value.inspect}" end + def assert_optional(object, attribute) + object.send("#{attribute}=", nil) + object.valid? + assert !object.errors.invalid?(attribute) + end + private def uses_host(name) diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 797ebe9..2ffa5ae 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -34,7 +34,29 @@ class CommentTest < Test::Unit::TestCase end should 'record unauthenticated author' do - flunk 'not yet' + + assert_optional Comment.new, :name + assert_optional Comment.new, :email + + # if given name, require email + c1 = Comment.new + c1.name = 'My Name' + assert_mandatory c1, :email + + # if given email, require name + c2 = Comment.new + c2.email = 'my@email.com' + assert_mandatory c2, :name + end + + should 'accept either an authenticated or unauthenticated author' do + assert_mandatory Comment.new, :author_id + + c1 = Comment.new + c1.author = create_user('someperson').person + c1.name = 'my name' + c1.valid? + assert c1.errors.invalid?(:name) end end -- libgit2 0.21.2