2015-03-25 63 views
0

我將郵件作爲Rails控制器操作的一部分發送,並使用ActionMailer以及Resque :: Mailer。如何在Rails中使用ActionMailer和resque_mailer編寫郵件測試

我用什麼背景作業庫(已經使用Resque作爲其他事情)並不重要,但是如何編寫集成請求規範來提出請求來觸發這些電子郵件?

或者還有其他一些我可以寫的測試嗎?

回答

0

我不是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 protected]", "Jimmy Bean") 
    # when 
    post :signup, "Email" => "[email protected]", "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("[email protected]", "Jojo Binks") 
    end 

    it "should be set to be delivered to the email passed in" do 
    @email.should deliver_to("[email protected]") 
    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 
相關問題