playframework / playframework/play-mailer

Testing that an email is sent

Open
#73 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question status:backlog
Dominant language
Scala
Stars
252
Forks
73
Avg merge
1d 4h
Merged PRs (30d)
19

Description

Is there a way to test that an email is sent, and check the contents of it? Would be nice to do something like:

"A verification email is sent when the user has registered" in {
  MailPlugin.startCapturing()
  UserModule.registerNewUser(name = "Frodo", email = "frodo@example.com")
  val emails: List[Email] = MailPlugin.stopCapturingAndCollectResults()

  emails mustEqual List(
    Email(
      subject = "Welcome Frodo",
      recipient = "frodo@example.com",
      bodyText = "Here is your verifcation link: ______"
    )
  )
}

Or perhaps something more "functional"/safe:

"A verification email is sent when the user has registered" in {
  val emails: List[Email] = MailPlugin.captureMailsSentInBlock {
    UserModule.registerNewUser(name = "Frodo", email = "frodo@example.com")
  }

  emails mustEqual List(
    Email(
      subject = "Welcome Frodo",
      recipient = "frodo@example.com",
      bodyText = "Here is your verifcation link: ______"
    )
  )
}

Is it possible to do something like this at all right now? ie. Setting the plugin in testing/capture mode so that we can verify: (1) that en email is sent, (2) verify the contents of it.

Also, mock-mode must be on when running your test suite, right? (play.mailer.mock = yes)

I'm sorry if this is asked before, but I couldn't find anything in the manual or from searching.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting the MailPlugin API and the play.mailer.mock configuration mentioned in the issue. Determine how test-mode mail delivery currently works and define a supported way to capture sent emails, inspect their subject, recipients, and body, and verify that behavior in tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.