1

Email on Rails

 2 years ago
source link: https://www.mailslurp.com/blog/testing-real-emails-ruby-on-rails/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Email on Rails

Testing with real email addresses in Ruby (Rails, Rspec)

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:

source "https://rubygems.org"

gem 'mailslurp-client', '~> 1.0', '>= 1.0.1'

# other gems
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

# some pseudo code here
require 'test_helper'
require 'mailslurp-client'

# include and configure mailslurp
MailSlurpClient.configure do |config|
  config.api_key['x-api-key'] = 'your-key'
end

api_instance = MailSlurpClient::CommonOperationsApi.new

email_address = nil
verification_code = nil

class UserFlowTest < ActionDispatch::IntegrationTest

  test "can sign-up with mailslurp" do

    # create a new private email address for this test run
    result = api_instance.create_new_email_address_using_post
    email_address = result.email_address

    # sign up the user via rails endpoint
    post "/sign-up",
      params: { email_address: email_address }
    assert_response :success
  end

  test "user receives verification code" do
    # get the verification email from inbox and extract verification code
    email = api_instance.fetch_latest_email_using_get(email_address)
    verification_code = email.body.match(/code: ([0-9]{6})/)
  end

  test "can verify user account" do
    # verify the user using the code we received
    post "/verify",
      params: { email_address: email_address, code: verification_code }
    assert_response :success
  end
end
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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK