首页 > 解决方案 > 问:如何使用 Turnip、capybara 和 Gherkin 测试 ActionMailer Deliver_later

问题描述

def deliver_mail
  ServiceMailer.activation().deliver_later
end

从某个控制器调用deliver_mail 方法。

我想像下面这样进行测试 - 使用黄瓜和水豚进行功能测试。

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

实际上,当按下'push next button'时,邮件是通过deliver_mail方法发送的。

当我使用deliver_now 而不是deliver_later 时,我可以测试上面的代码。

但是在我将deliver_now 更改为deliver_later 后,我无法测试。

所以我在下面引用。

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

我试图在规范文件中包含 'ActiveJob::TestHelper' 之类的 'include ActiveJob::TestHelper'。

我修改了这样的步骤文件。

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

但仍然无法正常工作。

请提供任何提示和建议。

标签: ruby-on-railsruby-on-rails-5capybara-webkitrails-activejobturnip

解决方案


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.


推荐阅读