Blog An exploration of the art and
craft of software development

Testing Delayed Job email sends

Posted by Marty Haught on Wednesday, February 10, 2010

On one of my projects, we are using Delayed Job to send emails when someone posts a topic to a forum that many users can be montoring. Delayed Job is fantastic but as part of our test suite we had been testing the output of the notification email. When introducing Delayed Job that made the testing aspect trickier.

Below is our approach in testing this solution. The project is using Test:Unit so there is rspec goodness here. Also, we opted not to mock in this case.

On line 2 you can see we use the assert_difference with the Delayed::Job.count method to see that a row indeed gets added.

Line 6 uses the Rails assert_emails block as you usually would but to trigger the email send we use the work_off method to only send and then exit.

The remainder of the gist does usual pattern matching with the email body returned from ActionMailer::Base.deliveries.

blog comments powered by Disqus