I'm hoping this is the right place for feature suggestions/requests.
We have specific matchers that help us check what ActiveJob jobs have been queued (have_enqueued_job
), but we don't have similar things for ActionMailer.
I've done some looking around and it seems like this is the general way used to test that an email has been sent. This doesn't feel quite right because I'm not describing which email has been sent, and I need to dive into ActionMailer to get the number of deliveries.
expect {
post :create, params: { user: attributes_for(:user) }
}.to change(ActionMailer::Base.deliveries, :size).by(1)
It would be great if we had additional matchers for emails so that we could make more specific and descriptive specifications.
expect {
post :create, params: { user: attributes_for(:user) }
}.to have_enqueued_mailer(UserMailer, :welcome).with(User.first)
I hope I haven't missed existing functionality similar to this, I have had a good look around but couldn't find anything of the sort.
Better late than never, but this custom matcher should more or less do the trick:
RSpec::Matchers.define :have_enqueued_mailer do |mailer_klass, mailer_action|
match do |actual|
actual.call
@mailer_klass, @mailer_action, _, *@args = enqueued_jobs[0][:args]
@mailer_klass == mailer_klass.to_s &&
@mailer_action == mailer_action.to_s
end
chain :with do |*args|
@args == args
end
supports_block_expectations
end
Is this issue duplicate of #1901?
Yes @ybiquitous! :)
Thanks @cgat for the matcher.
Closing for #1901
Most helpful comment
Better late than never, but this custom matcher should more or less do the trick: