我正在尝试使用此 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") }
,我得到了同样的错误。