簡體   English   中英

RSpec Rails 郵件期望 - 最后一次交付是特定的操作/方法/模板

[英]RSpec Rails mailer expectation - last delivery is of specific action/method/template

我有以下任務:

我的郵件程序有多個動作/模板,每個都有相同的 static 硬編碼主題。

在我的 RSpec 期望中,我想驗證某些塊觸發特定郵件操作/模板的電子郵件(更改ActionMailer::Base.deliveries.last&.something )。

限制:

  • 不能依賴交付主題/標題(不是唯一的)
  • 最好避免檢查交付內容(除了一些小的措辭變化外,它們幾乎相同)
  • 不能僅僅為了使該規范通過而更改交付主題/標題生成
  • 不能模擬/存根郵件(我不想跳過渲染部分,這也是測試的一部分)

我嘗試過的方法不起作用:

ActionMailer::Base.deliveries.last.template_model
# => nil
ActionMailer::Base.deliveries.last.template_alias
# => nil
ActionMailer::Base.deliveries.last.filename
# => nil
ActionMailer::Base.deliveries.last.metadata
# => {}
ActionMailer::Base.deliveries.last.action
# => nil

完成這項任務的慣用 Rails 方式可能是什么? 自定義 header 與操作/模板名稱?

Mail::Message 方法(供參考):

ActionMailer::Base.deliveries.last.public_methods(false)
=> [:perform_deliveries=,
 :mime_version=,
 :sender,
 :metadata=,
 :boundary,
 :multipart?,
 :errors,
 :to_yaml,
 :delivery_handler,
 :tag,
 :raise_delivery_errors,
 :register_for_delivery_notification,
 :inform_observers,
 :inform_interceptors,
 :in_reply_to=,
 :comments=,
 :in_reply_to,
 :reply_to,
 :references=,
 :message_id=,
 :raw_source,
 :set_envelope,
 :filename,
 :raw_envelope,
 :envelope_from,
 :envelope_date,
 :content_description,
 :content_description=,
 :content_disposition=,
 :content_id=,
 :content_location,
 :content_location=,
 :content_transfer_encoding,
 :content_transfer_encoding=,
 :transport_encoding,
 :transport_encoding=,
 :received,
 :received=,
 :header=,
 :reply_to=,
 :resent_bcc,
 :resent_bcc=,
 :resent_cc,
 :resent_cc=,
 :resent_date,
 :resent_date=,
 :resent_from,
 :resent_from=,
 :bcc,
 :resent_message_id=,
 :resent_message_id,
 :resent_sender=,
 :resent_sender,
 :resent_to=,
 :resent_to,
 :references,
 :from,
 :return_path=,
 :sender=,
 :return_path,
 :smtp_envelope_from=,
 :smtp_envelope_to=,
 :smtp_envelope_from,
 :body_encoding,
 :body_encoding=,
 :smtp_envelope_to,
 :cc_addrs,
 :bcc_addrs,
 :from_addrs,
 :has_message_id?,
 :has_date?,
 :header_fields,
 :has_charset?,
 :has_content_transfer_encoding?,
 :has_mime_version?,
 :has_transfer_encoding?,
 :destinations,
 :add_date,
 :add_message_id,
 :add_mime_version,
 :sub_type,
 :to,
 :subject,
 :subject=,
 :cc=,
 :bcc=,
 :add_content_transfer_encoding,
 :perform_deliveries,
 :<=>,
 :message_content_type,
 :delivery_status_part,
 :add_content_type,
 :add_charset,
 :==,
 :main_type,
 :transfer_encoding,
 :diagnostic_code,
 :mime_parameters,
 :[],
 :[]=,
 :remote_mta,
 :retryable?,
 :html_part=,
 :bounced?,
 :final_recipient,
 :add_transfer_encoding,
 :text_part=,
 :convert_to_multipart,
 :attachment,
 :keywords,
 :charset=,
 :delivery_handler=,
 :without_attachments!,
 :error_status,
 :delivery_status_report?,
 :decode_body,
 :all_parts,
 :skip_deletion,
 :mark_for_delete=,
 :is_marked_for_delete?,
 :inspect,
 :multipart_report?,
 :method_missing,
 :attachment?,
 :mime_version,
 :add_file,
 :action,
 :raise_delivery_errors=,
 :parts,
 :to_s,
 :body=,
 :content_type_parameters,
 :has_attachments?,
 :add_part,
 :find_first_mime_type,
 :encoded,
 :date,
 :decoded,
 :attachments,
 :track_links,
 :part,
 :templated?,
 :metadata,
 :export_headers,
 :track_opens,
 :template_alias,
 :template_model,
 :message_stream,
 :export_attachments,
 :body_html,
 :body_text,
 :message_id,
 :comments,
 :to_addrs,
 :from=,
 :postmark_response,
 :default,
 :to_postmark_hash,
 :content_disposition,
 :content_id,
 :has_content_type?,
 :keywords=,
 :delivery_method,
 :date=,
 :text?,
 :cc,
 :delivered=,
 :postmark_response=,
 :encode!,
 :mime_type,
 :html?,
 :tag=,
 :delivered,
 :delivered?,
 :track_links=,
 :track_opens=,
 :postmark_attachments=,
 :postmark_attachments,
 :read,
 :message_stream=,
 :prerender,
 :text_part,
 :html_part,
 :reply,
 :ready_to_send!,
 :template_model=,
 :deliver!,
 :headers,
 :header,
 :content_type,
 :to=,
 :charset,
 :deliver,
 :body,
 :content_type=]

相關問題:

Rails,RSpec:如何測試特定郵件操作是否被觸發(使用模擬)

這就是我目前的結果(直到我找到更好的方法):

將以下行添加到每個相關的郵件程序操作中:

headers['X-Rails-Template'] = __method__

添加我的規格看起來有點像這樣:

subject { -> { described_class.call } }

let(:last_template_name) do
  -> do
    m = ActionMailer::Base.deliveries.last
    next if m.blank?

    m.header['X-Rails-Template']&.value
  end
end

it do
  is_expected.to change { ActionMailer::Base.deliveries.count }.to(1)
                   .and change(&last_template_name).to('mailer_action_name1')
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM