From 9d7e5085fd1eeaeed9798944874411be02e729ae Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Fri, 17 Aug 2012 14:43:55 -0300 Subject: [PATCH] Extract comment creation code into its own method --- test/unit/comment_notifier_test.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/test/unit/comment_notifier_test.rb b/test/unit/comment_notifier_test.rb index 33ebab2..491f44a 100644 --- a/test/unit/comment_notifier_test.rb +++ b/test/unit/comment_notifier_test.rb @@ -14,24 +14,24 @@ class CommentNotifierTest < ActiveSupport::TestCase should 'deliver mail after make an article comment' do assert_difference ActionMailer::Base.deliveries, :size do - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) end end should 'deliver mail to owner of article' do - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) sent = ActionMailer::Base.deliveries.first assert_equal [@profile.email], sent.to end should 'display author name in delivered mail' do - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article) + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article) sent = ActionMailer::Base.deliveries.first assert_match /user_comment_test/, sent.body end should 'display unauthenticated author name and email in delivered mail' do - Comment.create(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article ) + create_comment_and_notify(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article ) sent = ActionMailer::Base.deliveries.first assert_match /flatline/, sent.body assert_match /flatline@invalid.com/, sent.body @@ -40,18 +40,18 @@ class CommentNotifierTest < ActiveSupport::TestCase should 'not deliver mail if notify comments is false' do @article.update_attribute(:notify_comments, false) assert_no_difference ActionMailer::Base.deliveries, :size do - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article) end end should 'include comment title in the e-mail' do - Comment.create(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) + create_comment_and_notify(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) sent = ActionMailer::Base.deliveries.first assert_match /comment title/, sent.body end should 'include comment text in the e-mail' do - Comment.create(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) + create_comment_and_notify(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) sent = ActionMailer::Base.deliveries.first assert_match /comment body/, sent.body end @@ -61,7 +61,7 @@ class CommentNotifierTest < ActiveSupport::TestCase assert_equal [], community.notification_emails article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true) assert_no_difference ActionMailer::Base.deliveries, :size do - article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification') + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification', :source => article) end end @@ -70,18 +70,22 @@ class CommentNotifierTest < ActiveSupport::TestCase follower = create_user('follower').person @article.followers += [follower.email] @article.save! - @article.comments << Comment.new(:source => @article, :author => author, :title => 'comment title', :body => 'comment body') + create_comment_and_notify(:source => @article, :author => author, :title => 'comment title', :body => 'comment body') assert_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email end should "not deliver follower's mail about new comment to comment's author" do follower = create_user('follower').person - @article.comments << Comment.new(:source => @article, :author => follower, :title => 'comment title', :body => 'comment body') + create_comment_and_notify(:source => @article, :author => follower, :title => 'comment title', :body => 'comment body') assert_not_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email end private + def create_comment_and_notify(args) + Comment!.create(args) + end + def read_fixture(action) IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") end -- libgit2 0.21.2