簡體   English   中英

如何在Rails中使用ActionMailer和resque_mailer編寫電子郵件測試

[英]How to write tests for emails using ActionMailer and resque_mailer in Rails

我正在作為Rails控制器操作的一部分發送郵件,並且正在使用ActionMailer以及Resque :: Mailer。

我使用什么后台作業庫(已經使用Resque做其他事情)並不重要,但是我如何編寫集成請求規范以發出應該觸發這些電子郵件的請求?

還是我可以編寫其他測試?

我不確定100%是否能正確理解您的問題,但我相信您可以使用should_receive和email-spec來測試是否在特定控制器中發送了電子郵件以及這些電子郵件的內容。

例:

describe "POST /signup (#signup)" do
  it "should deliver the signup email" do
    # expect
    UserMailer.should_receive(:deliver_signup).with("email@example.com", "Jimmy Bean")
    # when
    post :signup, "Email" => "email@example.com", "Name" => "Jimmy Bean"
  end
end

范例2:

describe "Signup Email" do
  include EmailSpec::Helpers
  include EmailSpec::Matchers
  # include ActionController::UrlWriter - old rails
  include Rails.application.routes.url_helpers

  before(:all) do
    @email = UserMailer.create_signup("jojo@yahoo.com", "Jojo Binks")
  end

  it "should be set to be delivered to the email passed in" do
    @email.should deliver_to("jojo@yahoo.com")
  end

  it "should contain the user's message in the mail body" do
    @email.should have_body_text(/Jojo Binks/)
  end

  it "should contain a link to the confirmation link" do
    @email.should have_body_text(/#{confirm_account_url}/)
  end

  it "should have the correct subject" do
    @email.should have_subject(/Account confirmation/)
  end

end

暫無
暫無

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

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