Mailer Testing

Posted by PunNeng, Wed Feb 20 02:04:00 UTC 2008

Mailer Testing

ไปเจอมาจากที่นี่ ตัด code ที่ผมคิดว่าไม่จำเป็นออก
ในส่วนของ model หน้าตาก็จะได้ประมาณนี้

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
describe EmailSender do
  setup do
    @expected = TMail::Mail.new
    @expected.set_content_type 'text', 'plain', { 'charset' => "utf-8" }
    @expected.mime_version = '1.0'
  end

  it 'should create an email' do
    @expected.subject = 'Rspec'
    @expected.body    = 'Rspec is so cool.'
    @expected.from    = 'myemail@myemail.com'
    @expected.to      = 'youremail@youremail.com'

    EmailSender.create_your_email.encoded.should == @expected.encoded
  end
end

ทดสอบแค่ create พอ ไม่จำเป็นต้องส่ง ถ้าจะส่งก็แค่เรียก EmailSender.deliver(EmailSender.create_your_email)

แต่ปกติแล้วเราาจะใช้ EmailSender.deliveryouremail

ในฝั่ง controller ก็เซ็ตแบบปกติไป

  1
  2
  3
  4
  5
  6
describe YourController do
  it "should send email" do
    EmailSender.should_receive(:deliver_your_email)
    post :your_action
  end
end

พระเอกจริงๆ อยู่ที่ TMail นี่แหละครับ ตัว ActionMailer ก็ใช้ TMail เหมือนกัน

Filed Under: Ruby on Rails | Tags: behavior driven development mailer rspec ruby on rails

Comments

Have your say

A name is required. You may use HTML in your comments.




codegent: we're hiring