How to test email sending using Rspec?
What are the best practices and tools to test email-sending using rspec with Rails?
For instance, how do I test that an email has been sent or what should I test to have effici开发者_如何学Goent testing and acceptable coverage.
If you guys need an example, how would I go and test this:
class UserMailer < ActionMailer::Base
def jobdesc_has_been_reviewed(user, title)
@body[:title] = title
@body[:jobdesc] = jobdesc
@body[:url] = "http://" + user.account.subdomain + "." + Constants::SITE_URL + "/jobdescs/#{jobdesc.id}"
end
end
email-spec looks like a good library
http://github.com/bmabey/email-spec
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.
精彩评论