Commit 9d7e5085fd1eeaeed9798944874411be02e729ae
1 parent
d02d6e2c
Exists in
master
and in
28 other branches
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 | 14 | |
15 | 15 | should 'deliver mail after make an article comment' do |
16 | 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 | 18 | end |
19 | 19 | end |
20 | 20 | |
21 | 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 | 23 | sent = ActionMailer::Base.deliveries.first |
24 | 24 | assert_equal [@profile.email], sent.to |
25 | 25 | end |
26 | 26 | |
27 | 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 | 29 | sent = ActionMailer::Base.deliveries.first |
30 | 30 | assert_match /user_comment_test/, sent.body |
31 | 31 | end |
32 | 32 | |
33 | 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 | 35 | sent = ActionMailer::Base.deliveries.first |
36 | 36 | assert_match /flatline/, sent.body |
37 | 37 | assert_match /flatline@invalid.com/, sent.body |
... | ... | @@ -40,18 +40,18 @@ class CommentNotifierTest < ActiveSupport::TestCase |
40 | 40 | should 'not deliver mail if notify comments is false' do |
41 | 41 | @article.update_attribute(:notify_comments, false) |
42 | 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 | 44 | end |
45 | 45 | end |
46 | 46 | |
47 | 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 | 49 | sent = ActionMailer::Base.deliveries.first |
50 | 50 | assert_match /comment title/, sent.body |
51 | 51 | end |
52 | 52 | |
53 | 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 | 55 | sent = ActionMailer::Base.deliveries.first |
56 | 56 | assert_match /comment body/, sent.body |
57 | 57 | end |
... | ... | @@ -61,7 +61,7 @@ class CommentNotifierTest < ActiveSupport::TestCase |
61 | 61 | assert_equal [], community.notification_emails |
62 | 62 | article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true) |
63 | 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 | 65 | end |
66 | 66 | end |
67 | 67 | |
... | ... | @@ -70,18 +70,22 @@ class CommentNotifierTest < ActiveSupport::TestCase |
70 | 70 | follower = create_user('follower').person |
71 | 71 | @article.followers += [follower.email] |
72 | 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 | 74 | assert_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email |
75 | 75 | end |
76 | 76 | |
77 | 77 | should "not deliver follower's mail about new comment to comment's author" do |
78 | 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 | 80 | assert_not_includes ActionMailer::Base.deliveries.map(&:bcc).flatten, follower.email |
81 | 81 | end |
82 | 82 | |
83 | 83 | private |
84 | 84 | |
85 | + def create_comment_and_notify(args) | |
86 | + Comment!.create(args) | |
87 | + end | |
88 | + | |
85 | 89 | def read_fixture(action) |
86 | 90 | IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") |
87 | 91 | end | ... | ... |