mailslurp-examples - ruby-rspec

https://github.com/mailslurp/examples

Table of Contents

ruby-rspec/example.spec.rb

RSpec.describe 'client' do

  require 'mailslurp_client'

  # configure the mailslurp client with an API Key
  MailSlurpClient.configure do |config|
    config.api_key['x-api-key'] = ENV['API_KEY']
  end

  it 'can create email addresses' do
    inbox_controller = MailSlurpClient::InboxControllerApi.new
    inbox = inbox_controller.create_inbox

    expect(inbox.id).not_to be_nil
    expect(inbox.email_address).to include("mailslurp.com")
  end

  it 'can send an email' do
    # create an inbox
    inbox_controller = MailSlurpClient::InboxControllerApi.new
    inbox = inbox_controller.create_inbox

    # send an email from the inbox (to the inbox's own address)
    inbox_controller.send_email(inbox.id, {
        send_email_options: {
            to: [inbox.email_address],
            subject: "Test",
            isHTML: true,
            body: <<-HEREDOC
              <h1>Hello!</h1>
              <p>MailSlurp supports HTML</p>
            HEREDOC
        }
    })
  end

  it 'can receive emails and extract content' do
    # create two inboxes
    inbox_controller = MailSlurpClient::InboxControllerApi.new
    inbox_1 = inbox_controller.create_inbox
    inbox_2 = inbox_controller.create_inbox

    # send an email from inbox 1 to inbox 2 (sends a real email)
    inbox_controller.send_email(inbox_1.id, {
        send_email_options: {
            to: [inbox_2.email_address],
            subject: "Test",
            body: "Your code is 123",
        }
    })

    # get emails from inbox2
    waitfor_controller = MailSlurpClient::WaitForControllerApi.new
    email = waitfor_controller.wait_for_latest_email({ inbox_id: inbox_2.id, unread_only: true })

    # verify email contents
    expect(email.subject).to include("Test")
    expect(email.body).to include("Your code is")
  end
end

ruby-rspec/README.md

# Email testing
See [examples repository](https://github.com/mailslurp/examples) for source.

ruby-rspec/Makefile

-include ../.env

docker:
	docker build -t rspec --build-arg API_KEY=$(API_KEY) . && docker run rspec

vendor:
	bundle install --path ./vendor/bundle

test: vendor
	API_KEY=$(API_KEY) bundle exec rspec example.spec.rb

ruby-rspec/Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    diff-lcs (1.4.4)
    ethon (0.15.0)
      ffi (>= 1.15.0)
    ffi (1.15.4)
    mailslurp_client (15.0.2)
    rspec (3.10.0)
      rspec-core (~> 3.10.0)
      rspec-expectations (~> 3.10.0)
      rspec-mocks (~> 3.10.0)
    rspec-core (3.10.1)
      rspec-support (~> 3.10.0)
    rspec-expectations (3.10.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-mocks (3.10.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-support (3.10.2)
    typhoeus (1.4.0)
      ethon (>= 0.9.0)

PLATFORMS
  x86_64-linux

DEPENDENCIES
  mailslurp_client (~> 15.0, >= 15.0.2)
  rspec (~> 3.10.0)
  typhoeus (~> 1.4)

BUNDLED WITH
   2.2.29

ruby-rspec/Gemfile

source "https://rubygems.org"

gem 'rspec', '~> 3.10.0'
gem 'mailslurp_client', '~> 15.0', '>= 15.0.2'
gem 'typhoeus', '~> 1.4'

ruby-rspec/Dockerfile

FROM ruby:2.7.1-buster
ARG API_KEY
ENV API_KEY=$API_KEY
COPY . .
RUN ["rm", "Gemfile.lock"]
RUN ["bundle", "install", "--path", "./vendor/bundle"]
CMD ["bundle", "exec", "rspec", "example.spec.rb"]

ruby-rspec/.bundle/config

---
BUNDLE_PATH: "./vendor/bundle"