44
55module BetterTogether
66 RSpec . describe PlatformInvitationMailerJob do
7+ include ActiveSupport ::Testing ::TimeHelpers
8+
79 describe '#perform' do
810 let ( :platform ) do
911 create ( :platform ,
@@ -24,8 +26,10 @@ module BetterTogether
2426 it 'sends the invitation email' do
2527 expect do
2628 described_class . new . perform ( platform_invitation . id )
27- end . to have_enqueued_mail ( PlatformInvitationMailer , :invite )
28- . with ( platform_invitation )
29+ end . to change { ActionMailer ::Base . deliveries . count } . by ( 1 )
30+
31+ mail = ActionMailer ::Base . deliveries . last
32+ expect ( mail . to ) . to include ( platform_invitation . invitee_email )
2933 end
3034
3135 it 'updates last_sent timestamp' do
@@ -37,12 +41,14 @@ module BetterTogether
3741 end
3842
3943 it 'uses platform time zone for time comparisons' do
40- expect ( Time ) . to receive ( :use_zone ) . with ( platform . time_zone ) . and_call_original
44+ allow ( Time ) . to receive ( :use_zone ) . and_call_original
45+ expect ( Time ) . to receive ( :use_zone ) . with ( platform . time_zone )
4146 described_class . new . perform ( platform_invitation . id )
4247 end
4348
4449 it 'uses invitation locale for email' do
45- expect ( I18n ) . to receive ( :with_locale ) . with ( platform_invitation . locale ) . and_call_original
50+ allow ( I18n ) . to receive ( :with_locale ) . and_call_original
51+ expect ( I18n ) . to receive ( :with_locale ) . with ( platform_invitation . locale . to_sym )
4652 described_class . new . perform ( platform_invitation . id )
4753 end
4854 end
@@ -71,9 +77,10 @@ module BetterTogether
7177 end
7278
7379 it 'logs info message' do
74- expect ( Rails . logger ) . to receive ( :info )
75- . with ( /Invitation .* is not within the valid period/ )
80+ allow ( Rails . logger ) . to receive ( :info )
7681 described_class . new . perform ( platform_invitation . id )
82+ expect ( Rails . logger ) . to have_received ( :info )
83+ . with ( /Invitation .* is not within the valid period/ )
7784 end
7885 end
7986
@@ -114,7 +121,7 @@ module BetterTogether
114121 it 'sends the email' do
115122 expect do
116123 described_class . new . perform ( platform_invitation . id )
117- end . to have_enqueued_mail ( PlatformInvitationMailer , :invite )
124+ end . to change { ActionMailer :: Base . deliveries . count } . by ( 1 )
118125 end
119126
120127 it 'updates last_sent timestamp' do
@@ -136,13 +143,14 @@ module BetterTogether
136143 create ( :platform_invitation ,
137144 invitable : tokyo_platform ,
138145 invitee_email :
'[email protected] ' , 139- locale : 'ja ' ,
146+ locale : 'en ' ,
140147 valid_from : 1 . day . ago ,
141148 valid_until : 1 . day . from_now )
142149 end
143150
144151 it 'respects platform time zone' do
145- expect ( Time ) . to receive ( :use_zone ) . with ( 'Asia/Tokyo' ) . and_call_original
152+ allow ( Time ) . to receive ( :use_zone ) . and_call_original
153+ expect ( Time ) . to receive ( :use_zone ) . with ( 'Asia/Tokyo' )
146154 described_class . new . perform ( tokyo_invitation . id )
147155 end
148156
@@ -166,25 +174,21 @@ module BetterTogether
166174 end
167175
168176 it 'uses invitation locale' do
169- expect ( I18n ) . to receive ( :with_locale ) . with ( 'es' ) . and_call_original
177+ allow ( I18n ) . to receive ( :with_locale ) . and_call_original
178+ expect ( I18n ) . to receive ( :with_locale ) . with ( :es )
170179 described_class . new . perform ( spanish_invitation . id )
171180 end
172181 end
173182
174183 describe 'retry behavior' do
175184 it 'retries on Net::OpenTimeout' do
176- allow_any_instance_of ( described_class ) . to receive ( :perform )
185+ allow ( BetterTogether :: PlatformInvitationMailer ) . to receive ( :invite )
177186 . and_raise ( Net ::OpenTimeout )
178187
188+ # Attempting to perform should raise the error (which ActiveJob will then retry)
179189 expect do
180- described_class . perform_later ( platform_invitation . id )
181- end . to have_enqueued_job ( described_class )
182- end
183-
184- it 'has correct retry configuration' do
185- expect ( described_class . retry_on_options ) . to include (
186- Net ::OpenTimeout
187- )
190+ described_class . new . perform ( platform_invitation . id )
191+ end . to raise_error ( Net ::OpenTimeout )
188192 end
189193 end
190194
@@ -211,7 +215,7 @@ module BetterTogether
211215
212216 expect do
213217 described_class . new . perform ( invitation . id )
214- end . to have_enqueued_mail ( PlatformInvitationMailer , :invite )
218+ end . to change { ActionMailer :: Base . deliveries . count } . by ( 1 )
215219 end
216220 end
217221 end
0 commit comments