-2

I am trying to build an API server. For the register operation I send an email to the given email address and wait for user to activate his account by using the code in the email that i sent.

I use an external Email API for sending the email.

So of course I don't want this to happen in my development environment because,

  1. It is hard to manually test it. While using it on postman etc hard to check my email address all the time on a register.
  2. I don't want to waste money while using it with postman because the Email API is a paid service.

So what should be done?

2 Answers 2

3

As a first option, I would try to create an alternative API provider for the Email service, which doesn't really send a email but rather prints the relevant information to a file or the console (in a way that makes it easy for you to retrieve the activation link/code).

If that is not feasible, I would mock out the Email API in the application and replace it with something that prints the relevant information to a file or the console. A third version of that code could be used for validations in automated tests.

That should cover most of your development and testing needs. Be sure to do an integration test once in a while with the real service to ensure that still works.

4
  • So is it ok to write an if block to check the current environment (production or development) and use the real API or mocked API? Do I just leave that if block and the mocked API code in production ready git branch? Because it feels wrong to leave unused code since the mock API will never run in reality. In production environment. Is it fine? Commented Jun 4, 2020 at 11:54
  • 1
    @Mansur, Yes I would find that acceptable, just like I wouldn't delete automated tests from my repository, even if it is a production-ready branch and those tests never get executed in the production environment. Commented Jun 4, 2020 at 12:24
  • No I am not talking automated tests. Sometimes I use the application manually by using postman etc. tools. Commented Jun 4, 2020 at 12:30
  • 2
    I would use a configuration setting somewhere (probably in a config file) that would cause the application to choose between backend Emailer implementations. This way you can on your dev box choose the backend to use, allowing you to use the real service for a Live, but contained test, sending yourself an activation code. Or alternately use the implementation that writes the email to a local folder. In production if the real email service goes down, you could quickly reconfigure the app to spool the emails to a folder and use another method for dispatching them (as a last resort).
    – Kain0_0
    Commented Jun 5, 2020 at 6:31
3

In testing, you should give yourself the capability to "mock out" the email-sending functionality, so that you can write the email text elsewhere, without actually having to send them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.