Commit 9d7e5085fd1eeaeed9798944874411be02e729ae

Authored by Antonio Terceiro
1 parent d02d6e2c

Extract comment creation code into its own method

Showing 1 changed file with 14 additions and 10 deletions   Show diff stats
test/unit/comment_notifier_test.rb
@@ -14,24 +14,24 @@ class CommentNotifierTest < ActiveSupport::TestCase @@ -14,24 +14,24 @@ class CommentNotifierTest < ActiveSupport::TestCase
14 14
15 should 'deliver mail after make an article comment' do 15 should 'deliver mail after make an article comment' do
16 assert_difference ActionMailer::Base.deliveries, :size do 16 assert_difference ActionMailer::Base.deliveries, :size do
17 - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) 17 + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article )
18 end 18 end
19 end 19 end
20 20
21 should 'deliver mail to owner of article' do 21 should 'deliver mail to owner of article' do
22 - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article ) 22 + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article )
23 sent = ActionMailer::Base.deliveries.first 23 sent = ActionMailer::Base.deliveries.first
24 assert_equal [@profile.email], sent.to 24 assert_equal [@profile.email], sent.to
25 end 25 end
26 26
27 should 'display author name in delivered mail' do 27 should 'display author name in delivered mail' do
28 - Comment.create(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article) 28 + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article)
29 sent = ActionMailer::Base.deliveries.first 29 sent = ActionMailer::Base.deliveries.first
30 assert_match /user_comment_test/, sent.body 30 assert_match /user_comment_test/, sent.body
31 end 31 end
32 32
33 should 'display unauthenticated author name and email in delivered mail' do 33 should 'display unauthenticated author name and email in delivered mail' do
34 - Comment.create(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article ) 34 + create_comment_and_notify(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article )
35 sent = ActionMailer::Base.deliveries.first 35 sent = ActionMailer::Base.deliveries.first
36 assert_match /flatline/, sent.body 36 assert_match /flatline/, sent.body
37 assert_match /flatline@invalid.com/, sent.body 37 assert_match /flatline@invalid.com/, sent.body
@@ -40,18 +40,18 @@ class CommentNotifierTest < ActiveSupport::TestCase @@ -40,18 +40,18 @@ class CommentNotifierTest < ActiveSupport::TestCase
40 should 'not deliver mail if notify comments is false' do 40 should 'not deliver mail if notify comments is false' do
41 @article.update_attribute(:notify_comments, false) 41 @article.update_attribute(:notify_comments, false)
42 assert_no_difference ActionMailer::Base.deliveries, :size do 42 assert_no_difference ActionMailer::Base.deliveries, :size do
43 - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') 43 + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'you suck!', :source => @article)
44 end 44 end
45 end 45 end
46 46
47 should 'include comment title in the e-mail' do 47 should 'include comment title in the e-mail' do
48 - Comment.create(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) 48 + create_comment_and_notify(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article)
49 sent = ActionMailer::Base.deliveries.first 49 sent = ActionMailer::Base.deliveries.first
50 assert_match /comment title/, sent.body 50 assert_match /comment title/, sent.body
51 end 51 end
52 52
53 should 'include comment text in the e-mail' do 53 should 'include comment text in the e-mail' do
54 - Comment.create(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article) 54 + create_comment_and_notify(:author => @profile, :title => 'comment title', :body => 'comment body', :source => @article)
55 sent = ActionMailer::Base.deliveries.first 55 sent = ActionMailer::Base.deliveries.first
56 assert_match /comment body/, sent.body 56 assert_match /comment body/, sent.body
57 end 57 end
@@ -61,7 +61,7 @@ class CommentNotifierTest &lt; ActiveSupport::TestCase @@ -61,7 +61,7 @@ class CommentNotifierTest &lt; ActiveSupport::TestCase
61 assert_equal [], community.notification_emails 61 assert_equal [], community.notification_emails
62 article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true) 62 article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true)
63 assert_no_difference ActionMailer::Base.deliveries, :size do 63 assert_no_difference ActionMailer::Base.deliveries, :size do
64 - article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification') 64 + create_comment_and_notify(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification', :source => article)
65 end 65 end
66 end 66 end
67 67
@@ -70,18 +70,22 @@ class CommentNotifierTest &lt; ActiveSupport::TestCase @@ -70,18 +70,22 @@ class CommentNotifierTest &lt; ActiveSupport::TestCase
70 follower = create_user('follower').person 70 follower = create_user('follower').person
71 @article.followers += [follower.email] 71 @article.followers += [follower.email]
72 @article.save! 72 @article.save!
73 - @article.comments << Comment.new(:source => @article, :author => author, :title => 'comment title', :body => 'comment body') 73 + create_comment_and_notify(:source => @article, :author => author, :title => 'comment title', :body => 'comment body')
74 assert_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email 74 assert_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email
75 end 75 end
76 76
77 should "not deliver follower's mail about new comment to comment's author" do 77 should "not deliver follower's mail about new comment to comment's author" do
78 follower = create_user('follower').person 78 follower = create_user('follower').person
79 - @article.comments << Comment.new(:source => @article, :author => follower, :title => 'comment title', :body => 'comment body') 79 + create_comment_and_notify(:source => @article, :author => follower, :title => 'comment title', :body => 'comment body')
80 assert_not_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email 80 assert_not_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email
81 end 81 end
82 82
83 private 83 private
84 84
  85 + def create_comment_and_notify(args)
  86 + Comment!.create(args)
  87 + end
  88 +
85 def read_fixture(action) 89 def read_fixture(action)
86 IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") 90 IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}")
87 end 91 end