Commit 4d4016e5c3295a1a64991b46fefcc0941c407079
1 parent
742c74a4
Exists in
master
and in
29 other branches
person-notifier-test: avoid more randomness
Showing
1 changed file
with
13 additions
and
4 deletions
Show diff stats
test/unit/person_notifier_test.rb
@@ -25,6 +25,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -25,6 +25,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
25 | 25 | ||
26 | should 'deliver mail to community members' do | 26 | should 'deliver mail to community members' do |
27 | @community.add_member(@member) | 27 | @community.add_member(@member) |
28 | + process_delayed_job_queue | ||
28 | notify | 29 | notify |
29 | sent = ActionMailer::Base.deliveries.first | 30 | sent = ActionMailer::Base.deliveries.first |
30 | assert_equal [@member.email], sent.to | 31 | assert_equal [@member.email], sent.to |
@@ -48,8 +49,9 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -48,8 +49,9 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
48 | should 'display author name in delivered mail' do | 49 | should 'display author name in delivered mail' do |
49 | @community.add_member(@member) | 50 | @community.add_member(@member) |
50 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) | 51 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) |
52 | + process_delayed_job_queue | ||
51 | notify | 53 | notify |
52 | - sent = ActionMailer::Base.deliveries.first | 54 | + sent = ActionMailer::Base.deliveries.last |
53 | assert_match /#{@admin.name}/, sent.body | 55 | assert_match /#{@admin.name}/, sent.body |
54 | end | 56 | end |
55 | 57 | ||
@@ -81,8 +83,11 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -81,8 +83,11 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
81 | 83 | ||
82 | should 'schedule next mail at notification time' do | 84 | should 'schedule next mail at notification time' do |
83 | @member.notification_time = 12 | 85 | @member.notification_time = 12 |
86 | + time = Time.now | ||
84 | @member.notifier.schedule_next_notification_mail | 87 | @member.notifier.schedule_next_notification_mail |
85 | - assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour | 88 | + job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last |
89 | + assert job.run_at >= time + @member.notification_time.hours | ||
90 | + assert job.run_at < time + (@member.notification_time+1).hours | ||
86 | end | 91 | end |
87 | 92 | ||
88 | should 'do not schedule duplicated notification mail' do | 93 | should 'do not schedule duplicated notification mail' do |
@@ -130,6 +135,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -130,6 +135,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
130 | end | 135 | end |
131 | 136 | ||
132 | should 'reschedule with changed notification time' do | 137 | should 'reschedule with changed notification time' do |
138 | + time = Time.now | ||
133 | assert_difference Delayed::Job, :count, 1 do | 139 | assert_difference Delayed::Job, :count, 1 do |
134 | @member.notification_time = 2 | 140 | @member.notification_time = 2 |
135 | @member.save! | 141 | @member.save! |
@@ -138,13 +144,17 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -138,13 +144,17 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
138 | @member.notification_time = 12 | 144 | @member.notification_time = 12 |
139 | @member.save! | 145 | @member.save! |
140 | end | 146 | end |
141 | - assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour | 147 | + @member.notifier.schedule_next_notification_mail |
148 | + job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last | ||
149 | + assert job.run_at >= time + @member.notification_time.hours | ||
150 | + assert job.run_at < time + (@member.notification_time+1).hours | ||
142 | end | 151 | end |
143 | 152 | ||
144 | should 'display error message if fail to render a notificiation' do | 153 | should 'display error message if fail to render a notificiation' do |
145 | @community.add_member(@member) | 154 | @community.add_member(@member) |
146 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) | 155 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) |
147 | ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb") | 156 | ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb") |
157 | + process_delayed_job_queue | ||
148 | notify | 158 | notify |
149 | sent = ActionMailer::Base.deliveries.last | 159 | sent = ActionMailer::Base.deliveries.last |
150 | assert_match /cannot render notification for some_invalid_verb/, sent.body | 160 | assert_match /cannot render notification for some_invalid_verb/, sent.body |
@@ -217,7 +227,6 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -217,7 +227,6 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
217 | end | 227 | end |
218 | 228 | ||
219 | def notify | 229 | def notify |
220 | - process_delayed_job_queue | ||
221 | @member.notifier.notify | 230 | @member.notifier.notify |
222 | end | 231 | end |
223 | 232 |