Commit 6cd4e874e3e56953d1047eeb2b0322d8d0af422a

Authored by Rodrigo Souto
1 parent 26bfd338

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 33 should 'do not send mail if do not have notifications' do
34 34 @community.add_member(@member)
35 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 39 end
39 40  
40 41 should 'do not send mail to people not joined to community' do
41 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 46 end
45 47  
46 48 should 'display author name in delivered mail' do
... ... @@ -56,8 +58,9 @@ class PersonNotifierTest < ActiveSupport::TestCase
56 58 ActionTracker::Record.delete_all
57 59 comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article )
58 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 64 end
62 65  
63 66 should 'update last notification date' do
... ... @@ -85,14 +88,16 @@ class PersonNotifierTest < ActiveSupport::TestCase
85 88 should 'do not schedule duplicated notification mail' do
86 89 @member.notification_time = 12
87 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 94 end
91 95  
92 96 should 'do not schedule next mail if notification time is zero' do
93 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 101 end
97 102  
98 103 should 'schedule next notifications for all person with notification time greater than zero' do
... ... @@ -108,26 +113,31 @@ class PersonNotifierTest < ActiveSupport::TestCase
108 113  
109 114 should 'do not create duplicated job' do
110 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 119 end
114 120  
115 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 130 end
123 131  
124 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 141 assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour
132 142 end
133 143  
... ... @@ -136,7 +146,7 @@ class PersonNotifierTest < ActiveSupport::TestCase
136 146 Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article)
137 147 ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb")
138 148 notify
139   - sent = ActionMailer::Base.deliveries.first
  149 + sent = ActionMailer::Base.deliveries.last
140 150 assert_match /cannot render notification for some_invalid_verb/, sent.body
141 151 end
142 152  
... ... @@ -157,7 +167,7 @@ class PersonNotifierTest < ActiveSupport::TestCase
157 167 Person.any_instance.stubs(:tracked_notifications).returns(notifications)
158 168  
159 169 notify
160   - sent = ActionMailer::Base.deliveries.first
  170 + sent = ActionMailer::Base.deliveries.last
161 171 assert_no_match /cannot render notification for #{verb}/, sent.body
162 172 end
163 173 end
... ... @@ -177,9 +187,10 @@ class PersonNotifierTest < ActiveSupport::TestCase
177 187 end
178 188  
179 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 194 end
184 195  
185 196 should 'perform create NotifyJob for all users with notification_time defined greater than zero' do
... ...