1
def deliver_mail
  ServiceMailer.activation().deliver_later
end

deliver_mail method is called from some controller.

I want to test like below - feature test using cucumber and capybara.

step 'push next button' do find("input.submit").click end

Feature: Sending a mail to user
  Scenario: mail to a user
    When I push next button
    Then mail should be sent to a user

actually, when 'push next button' is pushed, mail is sent by deliver_mail method.

when I use deliver_now instead of deliver_later, I can test the code above.

but after I change deliver_now to deliver_later, I can not test.

so I referenced below.

http://chriswarren.github.io/rpsec/testing/2015/03/13/testing-emails-and-active-job-in-rspec-feature-tests.html

I tried to include 'ActiveJob::TestHelper' like 'include ActiveJob::TestHelper' in spec file.

and i modified step file like this.

step 'push next button' do
  perform_enqueued_jobs do
    find("input.submit").click
  end
end

but still doesn't work.

any hint and advice please.

Kouta Osabe
  • 121
  • 1
  • 10

2 Answers2

0

I am using Delayed::Job for sending emails in a background, so when I test sending emails, I have two separate kinds of tests:

1) rspec - to check email body (in general, the method inside mailer) and sending the email itself

2) cucumber - to check if the email job was queued

In cucumber, I have the following code:

Then(/^A welcome email will be sent$/) do
  expect(Delayed::Job.count).to eq(1)
end

I think there is no need to check anything else from the cucumber.

MrShemek
  • 2,413
  • 1
  • 16
  • 20
0

I would recommend not delaying jobs in test, and then checking the results of whatever DJ was supposed to run.

# rails_helper.rb

Delayed::Worker.delay_jobs = false
hoffmanc
  • 614
  • 9
  • 16