Commit 6cd4e874e3e56953d1047eeb2b0322d8d0af422a
1 parent
26bfd338
Exists in
master
and in
28 other branches
person-notifier-test: avoid randomness on tests
Instead of testing the exact values of jobs and activities, I'm asserting the difference during a block run. This is much more precise and avoid context trash that makes the tests fail.
Showing
1 changed file
with
40 additions
and
29 deletions
Show diff stats
test/unit/person_notifier_test.rb
@@ -33,14 +33,16 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -33,14 +33,16 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
33 | should 'do not send mail if do not have notifications' do | 33 | should 'do not send mail if do not have notifications' do |
34 | @community.add_member(@member) | 34 | @community.add_member(@member) |
35 | ActionTracker::Record.delete_all | 35 | ActionTracker::Record.delete_all |
36 | - notify | ||
37 | - assert ActionMailer::Base.deliveries.empty? | 36 | + assert_no_difference ActionMailer::Base.deliveries, :count do |
37 | + notify | ||
38 | + end | ||
38 | end | 39 | end |
39 | 40 | ||
40 | should 'do not send mail to people not joined to community' do | 41 | should 'do not send mail to people not joined to community' do |
41 | Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) | 42 | Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) |
42 | - notify | ||
43 | - assert ActionMailer::Base.deliveries.blank? | 43 | + assert_no_difference ActionMailer::Base.deliveries, :count do |
44 | + notify | ||
45 | + end | ||
44 | end | 46 | end |
45 | 47 | ||
46 | should 'display author name in delivered mail' do | 48 | should 'display author name in delivered mail' do |
@@ -56,8 +58,9 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -56,8 +58,9 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
56 | ActionTracker::Record.delete_all | 58 | ActionTracker::Record.delete_all |
57 | comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article ) | 59 | comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article ) |
58 | @member.last_notification = DateTime.now + 1.day | 60 | @member.last_notification = DateTime.now + 1.day |
59 | - notify | ||
60 | - assert ActionMailer::Base.deliveries.empty? | 61 | + assert_no_difference ActionMailer::Base.deliveries, :count do |
62 | + notify | ||
63 | + end | ||
61 | end | 64 | end |
62 | 65 | ||
63 | should 'update last notification date' do | 66 | should 'update last notification date' do |
@@ -85,14 +88,16 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -85,14 +88,16 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
85 | should 'do not schedule duplicated notification mail' do | 88 | should 'do not schedule duplicated notification mail' do |
86 | @member.notification_time = 12 | 89 | @member.notification_time = 12 |
87 | @member.notifier.schedule_next_notification_mail | 90 | @member.notifier.schedule_next_notification_mail |
88 | - @member.notifier.schedule_next_notification_mail | ||
89 | - assert_equal 1, Delayed::Job.count | 91 | + assert_no_difference Delayed::Job, :count do |
92 | + @member.notifier.schedule_next_notification_mail | ||
93 | + end | ||
90 | end | 94 | end |
91 | 95 | ||
92 | should 'do not schedule next mail if notification time is zero' do | 96 | should 'do not schedule next mail if notification time is zero' do |
93 | @member.notification_time = 0 | 97 | @member.notification_time = 0 |
94 | - @member.notifier.schedule_next_notification_mail | ||
95 | - assert_equal 0, Delayed::Job.count | 98 | + assert_no_difference Delayed::Job, :count do |
99 | + @member.notifier.schedule_next_notification_mail | ||
100 | + end | ||
96 | end | 101 | end |
97 | 102 | ||
98 | should 'schedule next notifications for all person with notification time greater than zero' do | 103 | should 'schedule next notifications for all person with notification time greater than zero' do |
@@ -108,26 +113,31 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -108,26 +113,31 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
108 | 113 | ||
109 | should 'do not create duplicated job' do | 114 | should 'do not create duplicated job' do |
110 | PersonNotifier.schedule_all_next_notification_mail | 115 | PersonNotifier.schedule_all_next_notification_mail |
111 | - PersonNotifier.schedule_all_next_notification_mail | ||
112 | - assert_equal 1, Delayed::Job.count | 116 | + assert_no_difference Delayed::Job, :count do |
117 | + PersonNotifier.schedule_all_next_notification_mail | ||
118 | + end | ||
113 | end | 119 | end |
114 | 120 | ||
115 | should 'schedule after update and set a valid notification time' do | 121 | should 'schedule after update and set a valid notification time' do |
116 | - @member.notification_time = 0 | ||
117 | - @member.save! | ||
118 | - assert_equal 0, Delayed::Job.count | ||
119 | - @member.notification_time = 12 | ||
120 | - @member.save! | ||
121 | - assert_equal 1, Delayed::Job.count | 122 | + assert_no_difference Delayed::Job, :count do |
123 | + @member.notification_time = 0 | ||
124 | + @member.save! | ||
125 | + end | ||
126 | + assert_difference Delayed::Job, :count, 1 do | ||
127 | + @member.notification_time = 12 | ||
128 | + @member.save! | ||
129 | + end | ||
122 | end | 130 | end |
123 | 131 | ||
124 | should 'reschedule with changed notification time' do | 132 | should 'reschedule with changed notification time' do |
125 | - @member.notification_time = 2 | ||
126 | - @member.save! | ||
127 | - assert_equal 1, Delayed::Job.count | ||
128 | - @member.notification_time = 12 | ||
129 | - @member.save! | ||
130 | - assert_equal 1, Delayed::Job.count | 133 | + assert_difference Delayed::Job, :count, 1 do |
134 | + @member.notification_time = 2 | ||
135 | + @member.save! | ||
136 | + end | ||
137 | + assert_no_difference Delayed::Job, :count do | ||
138 | + @member.notification_time = 12 | ||
139 | + @member.save! | ||
140 | + end | ||
131 | assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour | 141 | assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour |
132 | end | 142 | end |
133 | 143 | ||
@@ -136,7 +146,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -136,7 +146,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
136 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) | 146 | Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) |
137 | ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb") | 147 | ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb") |
138 | notify | 148 | notify |
139 | - sent = ActionMailer::Base.deliveries.first | 149 | + sent = ActionMailer::Base.deliveries.last |
140 | assert_match /cannot render notification for some_invalid_verb/, sent.body | 150 | assert_match /cannot render notification for some_invalid_verb/, sent.body |
141 | end | 151 | end |
142 | 152 | ||
@@ -157,7 +167,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -157,7 +167,7 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
157 | Person.any_instance.stubs(:tracked_notifications).returns(notifications) | 167 | Person.any_instance.stubs(:tracked_notifications).returns(notifications) |
158 | 168 | ||
159 | notify | 169 | notify |
160 | - sent = ActionMailer::Base.deliveries.first | 170 | + sent = ActionMailer::Base.deliveries.last |
161 | assert_no_match /cannot render notification for #{verb}/, sent.body | 171 | assert_no_match /cannot render notification for #{verb}/, sent.body |
162 | end | 172 | end |
163 | end | 173 | end |
@@ -177,9 +187,10 @@ class PersonNotifierTest < ActiveSupport::TestCase | @@ -177,9 +187,10 @@ class PersonNotifierTest < ActiveSupport::TestCase | ||
177 | end | 187 | end |
178 | 188 | ||
179 | should 'perform create NotifyJob for all users with notification_time' do | 189 | should 'perform create NotifyJob for all users with notification_time' do |
180 | - Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) | ||
181 | - process_delayed_job_queue | ||
182 | - assert_equal 2, Delayed::Job.count | 190 | + assert_difference Delayed::Job, :count, 2 do |
191 | + Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) | ||
192 | + process_delayed_job_queue | ||
193 | + end | ||
183 | end | 194 | end |
184 | 195 | ||
185 | should 'perform create NotifyJob for all users with notification_time defined greater than zero' do | 196 | should 'perform create NotifyJob for all users with notification_time defined greater than zero' do |