Testing Email Deliveries using Cucumber/Capybara/Minitest


# features/support/env.rb
Around([email protected]') do |scenario, block|
  ActionMailer::Base.delivery_method = :test
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.deliveries.clear

  old_adapter = ActiveJob::Base.queue_adapter
  ActiveJob::Base.queue_adapter = :inline
  # ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
  block.call

  ActiveJob::Base.queue_adapter = old_adapter
end


# features/steps/*_step.rb

Then(/^I should receive an email$/) do
  # assuming mailcatcher
  assert_difference -> { ActionMailer::Base.deliveries.length } do
    # do stuff that is supposed to send an email
  end
end


# features/*.feature

@email
Scenario: Checkout page
  When I press the checkout button
  Then I should receive an email