ruby-on-rails – 测试电子邮件:通过后台工作人员发送的最后一
我正在尝试使用
this railscast为电子邮件添加一些测试,但是我正在为nil获取未定义的方法’to’:NilClass.
这些电子邮件实际上是通过后台工作人员使用Sidekiq进行处理的,我知道他们在生产中正常发送,他们也会在我向spec_helper.rb添加require’sidekiq / testing’之前发送 所以基本上我的问题是我如何访问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 当我使用指定{ActionMailer :: Base.deliveries.last.to.should include(“admin@email.com”)}时,我得到相同的错误. 解决方法
当需要sidekiq / testing而不是sidekiq / 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |