How to test email sending using Rspec? How to test email sending using Rspec? ruby ruby

How to test email sending using Rspec?


What do you want to test exactly?

Do you want to test that the email is indeed really sent?

I don't recommend it as it will make your test slower and it really depends on the machine you are using to run your tests (network connection, smtp server).

What I usually do is make sure the controller action / model / rake task which send emails runs correctly (meaning without error, I stub the final send call).I also make sure the body, title and recipients for the mail are correct.

Edit:

Not directly related to testing but I just came over this article.

I like his point of view of using the model to send emails.So to explain a little more in details:

I would use the controller to set up view variables like@body, @title, @recipient.

So I would test that given correct parameters the mail template (which is a view) is correctly rendered.This is were mistakes can happen from my point of view.

I will update this post with example when I am back home later today.