jRuby Test Automation: Checking to see that an email has been sent. What libraries are available?

StackOverflow https://stackoverflow.com/questions/20998522

  •  25-09-2022
  •  | 
  •  

Question

Background

I'm doing jRuby tests for a Grails app that sends emails. These tests are replacing tests that are normally done by manual testers. They check their inboxes to see that mail has been delivered.

Problem Statement

I want to do a similarly strong test that ensures the email is sent properly by the app. I do not need any actual code to help do this necessarily, but it would help to know what technologies to use.

Why I've Been Stumped So Far

I've looked for solutions in the Ruby domain, but everybody that I've seen address this problem seems to be using Rails. I am not using action_mailer or pony (we have a grails app) so I can't use email_spec. If I could that would be great.

What can I do?

Was it helpful?

Solution

You can use the Rails approach to testing mails: monkey-patch mail send code during test setup to place all mail instances that should have been delivered into a globally accessible array, this way you can do asserts on that array.

Don't ask me for exact implementation details because I'm not into grails, but the general principle is this:

if you have a class MyMailThingy which has a deliver method on it's instance which performs the mail delivery (presumably mail subject, body etc are attributes on this object), then monkey patch:

class MyMailThingy
  # same as `cattr_accessor :mock_queue` under rails
  def self.mock_queue; @@mock_queue; end
  def self.mock_queue=(arg); @@mock_queue=arg; end

  def deliver
    self.class.mock_queue ||= []
    self.class.mock_queue << self.dup
    self
  end
end

and then in your tests you can assert on MyMailThingy.mock_queue how many mails have been sent, assert on their bodies, subjects, to/from fields etc without sending actual mails

OTHER TIPS

I found an answer on how to send the mail here. It seems like Ruby has built in support for SMTP. I am looking at how to check the mail now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top