Ruby on Rails makes building and testing applications a breeze. But when it comes to user sign-up and email verification how do we test it? We can mock the email steps in tests or test it manually with our personal addresses but wouldn't it be better if we could test automatically with real email addresses? Imagine if we could smoke test this aspect of our app so that we know for sure that essential email related processes are behaving as we expect them to.
Email addresses APIs
Luckily test email account services like MailSlurp provide an easy way to create real email addresses on demand during tests. We can use this email address to sign up a new user. We can then get the last email they received and extract a verification code from it. Lastly we can use that real verification code to verify the user. All in all MailSlurp can help us test the full user sign up process end-to-end using real email addresses.
Install via RubyGems
Let's look at a real-world Ruby on Rails example. MailSlurp has an official Ruby client on RubyGems. We can add this to our Gemfile like so:
Testing user sign-up with real emails
Let's imagine a ROR app with a user controller that lets users sign up with an email address. Before the user can take actions they must also verify their account. Many apps do this, typically by sending the new user a verification code that they must submit to the app before they can proceed as a full user. Let's imagine an app that behaves this way and how we might test it.
In our user sign up integration test we might do the following:
- create a new email address with MailSlurp
- use the email address to sign up a user in Rails
- receive the user's verification email and check that a code is present
- extract and submit the verification code to complete the user sign up
An RSpec example
Here's what a Rails test might look like using RSpec
The importance of end-to-end testing
Unit tests are great but to determine if your app is truly functioning you need integration tests. Testing user sign up is usually tricky but with MailSlurp you can create unlimited private email address to send and receive real emails with. Check out mailslurp.com/developers/ for more information.