4

我正在尝试使用此 railscast添加一些电子邮件测试,但是我得到了undefined method 'to' for nil:NilClass.

这些电子邮件实际上是通过使用Sidekiq的后台工作人员处理的,我知道它们可以正确发送,因为它们在生产中工作,而且它们会在我添加require 'sidekiq/testing'到之前发送spec_helper.rb

所以基本上我的问题是如何访问 Sidekiq 中的电子邮件?我试图让工作人员处理电子邮件,但仍然遇到同样的错误。

规范/支持/mailer_macros.rb

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    ActionMailer::Base.deliveries = []
  end
end

规范/spec_helper.rb

require 'sidekiq/testing'
...
config.include MailerMacros
config.before(:each) { reset_email }

配置/环境/test.rb

config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'localhost:8080' }

控制器/tickets_controller.rb

def create
  @ticket = current_user.tickets.build(params[:ticket])   
  if @ticket.save
    Sidekiq::Client.enqueue(TicketNotifier, @ticket.id)
    flash[:success] = "Your ticket has been submitted successfully."
    redirect_to ticket_path(@ticket)
  else
    render 'new'
  end
end

规范的一部分:

require 'spec_helper'

describe "Tickets" do
  subject { page }

  describe "when creating a new ticket successfully" do
    before do
      login_as_user
      visit new_ticket_path
      fill_in "Subject", with: "Test ticket"
      fill_in "Description", with: "This is a test ticket"
      select 'Billing', from: "ticket[category]" 
      click_button "Submit Ticket"
    end

    specify { last_email.to.should include("admin@email.com") }
    # the rest tests the tickets#show view
    it { should have_selector('title', text: "Ticket #1") }
    it { should have_content("ticket has been submitted successfully") }
    ...
  end
end

当我改用时specify { ActionMailer::Base.deliveries.last.to.should include("admin@email.com") },我得到了同样的错误。

4

2 回答 2

5

当 requiressidekiq/testing和 notsidekiq/testing/inline时,作业被放入队列中,但在给出如下命令之前不会执行:

Sidekiq::Extensions::DelayedMailer.drain

我在我的规范中使用了这样的辅助方法:

def process_async
  EventWorker.drain
  Sidekiq::Extensions::DelayedMailer.drain
end

EventWorker 可能是我的应用程序中的自定义工作程序,并且Sidekiq::Extensions::DelayedMailer默认情况下是 Sidekiq 延迟邮件的位置。

然后我可以有这样的规范:

#do stuff that should send an email and trigger other sidekiq events
...
process_async
...
#assert the email was sent correctly

在 SideKiq wiki 上有一个很好的解释:https ://github.com/mperham/sidekiq/wiki/Testing

于 2013-04-02T13:00:01.520 回答
1

除了 cmaitchison 的回答之外,我还修复了它(在他们的 wiki 上:https ://github.com/mperham/sidekiq/wiki/Testing#rspec )

require 'sidekiq/testing'

RSpec.configure do |config|
  config.before(:each) do
    # Clears out the jobs for tests using the fake testing
    Sidekiq::Worker.clear_all

    if example.metadata[:sidekiq] == :fake
      Sidekiq::Testing.fake!
    elsif example.metadata[:sidekiq] == :inline
      Sidekiq::Testing.inline!
    elsif example.metadata[:type] == :acceptance
      Sidekiq::Testing.inline!
    else
      Sidekiq::Testing.fake!
    end
  end
end

并像这样进行测试:

describe "Tickets", :sidekiq => :inline do
  subject { page }

  describe "when creating a new ticket successfully" do
    before do
      login_as_user
      visit new_ticket_path
      fill_in "Subject", with: "Test ticket"
      fill_in "Description", with: "This is a test ticket"
      select 'Billing', from: "ticket[category]" 
      click_button "Submit Ticket"
    end

    specify { last_email.to.should include("admin@email.com") }
    # the rest tests the tickets#show view
    it { should have_selector('title', text: "Ticket #1") }
    it { should have_content("ticket has been submitted successfully") }
    ...
  end
end
于 2013-11-11T12:57:20.507 回答