Micah Gideon Modell
Micah Gideon Modell

Reputation: 629

Why can't I test email sending in Rails 5?

I'm trying to test email sending in a simple Rails 5 app using cucumber and using the ActionMailer guide and Testing guide with the following simple case. Can you help me see why it's not working?

app/mailers/test_mailer.rb

class TestMailer < ApplicationMailer
  def welcome_email
    @greeting = "Hi"
    mail to: "[email protected]", subject: 'welcome!'
  end
end

features/test.feature

Feature: test email

  Background:
    Given we say "hello"

  Scenario: send mail
    Given 0 email is in the queue
     Then send an email
    Given 1 email is in the queue

features/steps/test_steps.rb

Given "we say {string}"  do |say_it|
  puts say_it
end

Given "{int} email is in the queue"  do |mail_count|
  puts "method    : #{ActionMailer::Base.delivery_method}"
  puts "deliveries: #{ActionMailer::Base.perform_deliveries}"
  ActionMailer::Base.deliveries.count.should eq mail_count
end

Then "send an email"  do
  TestMailer.welcome_email.deliver_later
end

It keeps responding that there are no items in the queue.

Upvotes: 1

Views: 999

Answers (2)

Micah Gideon Modell
Micah Gideon Modell

Reputation: 629

As in my response to @diabolist, I had to modify my cucumber testing setup to support :async instead of :inline. This entailed:

config/environments/test.rb

...
config.active_job.queue.adapter = :test
...

features/support/env.rb

...
World( ActiveJob::TestHelper )
Around() do |scenario, block|
  perform_enqueued_jobs do
    block.call
  end
end

I realise I probably could have just switched my test adapter to :inline, but this will let me do some queue testing later — particularly with the performed methods.

Upvotes: 1

diabolist
diabolist

Reputation: 4099

Don't deliver_later, deliver right now. If you deliver_later you have to run your background jobs before your mail will be added to the queue

Upvotes: 1

Related Questions