Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matchers for delayed ActionMailer deliveries #1849

Closed
dwightwatson opened this issue Jul 22, 2017 · 3 comments
Closed

Matchers for delayed ActionMailer deliveries #1849

dwightwatson opened this issue Jul 22, 2017 · 3 comments

Comments

@dwightwatson
Copy link

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.

@cgat
Copy link

cgat commented Feb 7, 2018

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


@ybiquitous
Copy link

Is this issue duplicate of #1901?

@benoittgt
Copy link
Member

Yes @ybiquitous! :)

Thanks @cgat for the matcher.

Closing for #1901

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants