-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing that an email is sent #73
Comments
Hello, If you are using Play 2.4 then you can use dependency injection to provide a mock of MailerClient to your UserModule. With Guice (runtime dependency injection): val application = new GuiceApplicationBuilder()
.overrides(bind[MailerClient].to[MockMailerClient])
.build https://www.playframework.com/documentation/2.4.x/ScalaTestingWithGuice You can create a mock of MailerClient using Mockito or ScalaMock. Then you can assert that "A verification email is sent when the user has registered" in new WithApplication(application) {
UserModule.registerNewUser(name = "Frodo", email = "[email protected]")
Mockito.verify(UserModule.mailerClient).sendEmail(Email(
subject = "Welcome Frodo",
recipient = "[email protected]",
bodyText = "Here is your verifcation link: ______"
))
}
No Also in your test you want to do assertion. The
Don't apologize this is a very good question and I need to add documentation about testing. |
Thanks for swift reply :) Unfortunately, Play's support for dependency injection was a little too late for our project :( . We're already 2.5 years into a quite big play-project, so we don't have time to rewrite everything to use dependency injection. I would go for the solution you suggested above though when/if we start another play-project, but for now I made a small wrapper to work with our existing code:
It's not exactly 100% functional, but it does what must be done in order to test it. If anybody else is in the same situation as us, it's probably a good idea to set Anyway, just thought I'd post this here in case someone else has painted themselves into a DI-less-corner like we have. Here's how we use it in one of our tests:
(Edit: remember to replace every usage of the singleton |
Hello @drhumlen, thanks for sharing your findings. val mailerPluginMock = mockObject(MailerPlugin)
UserModule.registerNewUser(name = "Frodo", email = "[email protected]")
val expectedEmail = Email(
subject = "Welcome Frodo",
recipient = "[email protected]",
bodyText = "Here is your verifcation link: ______"
)
mailerPluginMock.expects.sendEmail(expectedEmail) The benefit is that you don't have to modify your code. |
Hi all -- placebocurejava has been banned from playframework, as he was posting on the google group with stolen credentials. His comments have been removed. Please see https://groups.google.com/forum/#!topic/play-framework/5FCht0abCI4 for more information. |
@drhumlen maybe you can use something like this https://github.com/mailhog/MailHog have it as docker and use its API to do your test. |
Is there a way to test that an email is sent, and check the contents of it? Would be nice to do something like:
Or perhaps something more "functional"/safe:
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.
The text was updated successfully, but these errors were encountered: