mailslurp-examples - ruby-cucumber-test

https://github.com/mailslurp/examples

Table of Contents

ruby-cucumber-test/README.md

# Cucumber MailSlurp Ruby
An example repository showing how to send and receive email in Cucumber tests using MailSlurp.

ruby-cucumber-test/Makefile

-include ../.env

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

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

test: vendor
	API_KEY=$(API_KEY) bundle exec cucumber

ruby-cucumber-test/Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    backports (3.15.0)
    builder (3.2.3)
    cucumber (3.1.2)
      builder (>= 2.1.2)
      cucumber-core (~> 3.2.0)
      cucumber-expressions (~> 6.0.1)
      cucumber-wire (~> 0.0.1)
      diff-lcs (~> 1.3)
      gherkin (~> 5.1.0)
      multi_json (>= 1.7.5, < 2.0)
      multi_test (>= 0.1.2)
    cucumber-core (3.2.1)
      backports (>= 3.8.0)
      cucumber-tag_expressions (~> 1.1.0)
      gherkin (~> 5.0)
    cucumber-expressions (6.0.1)
    cucumber-tag_expressions (1.1.1)
    cucumber-wire (0.0.1)
    diff-lcs (1.3)
    ethon (0.12.0)
      ffi (>= 1.3.0)
    ffi (1.11.1)
    gherkin (5.1.0)
    json (2.2.0)
    mailslurp_client (5.0.0)
      json (~> 2.1, >= 2.1.0)
      typhoeus (~> 1.0, >= 1.0.1)
    multi_json (1.13.1)
    multi_test (0.1.2)
    rspec (3.7.0)
      rspec-core (~> 3.7.0)
      rspec-expectations (~> 3.7.0)
      rspec-mocks (~> 3.7.0)
    rspec-core (3.7.1)
      rspec-support (~> 3.7.0)
    rspec-expectations (3.7.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.7.0)
    rspec-mocks (3.7.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.7.0)
    rspec-support (3.7.1)
    typhoeus (1.3.1)
      ethon (>= 0.9.0)

PLATFORMS
  ruby

DEPENDENCIES
  cucumber (~> 3.1.0)
  mailslurp_client (~> 5.0.0, >= 5.0.0)
  rspec (~> 3.7.0)

BUNDLED WITH
   2.0.1

ruby-cucumber-test/Gemfile

source "https://rubygems.org"

group :test do
  gem 'cucumber', '~> 3.1.0'
  gem 'rspec', '~> 3.7.0'
  gem 'mailslurp_client', '~> 5.0.0', '>= 5.0.0'
end

ruby-cucumber-test/Dockerfile

FROM ruby:2.7.1-buster
ARG API_KEY
ENV API_KEY=$API_KEY
COPY . .
RUN ["bundle", "install", "--path", "./vendor/bundle"]
CMD ["bundle", "exec", "cucumber"]

ruby-cucumber-test/features/can_i_send_and_receive_email.feature

Feature: Can I Send and Receive Email?
  Send and receive real emails in Cucumber using MailSlurp

  Scenario: Generate test email accounts
    Given a new email address
    When I ask for email address
    Then it should contain "@mailslurp.com"

  Scenario: Send a test email and receive it in code
    Given a new email address
    When I create new email
    When I set subject to "Hello"
    When I set body to "World"
    When I send email to created address
    Then I can receive email
    Then I can see "Hello" in subject
    Then I can see "World" in body

ruby-cucumber-test/features/support/env.rb

ruby-cucumber-test/features/step_definitions/stepdefs.rb

require 'mailslurp_client'

# Setup mailslurp
MailSlurpClient.configure do |config|
  config.api_key['x-api-key'] = ENV['API_KEY']
end

api_instance = MailSlurpClient::CommonOperationsApi.new
extra_instance = MailSlurpClient::ExtraOperationsApi.new

Given("a new email address") do
  @inbox = api_instance.create_new_email_address
end

When("I ask for email address") do
  @email_address = @inbox.email_address
end

Then("it should contain {string}") do |string|
    expect(@email_address).to include(string)
end

When("I create new email") do
    @email_options = MailSlurpClient::SendEmailOptions.new
end

When("I set subject to {string}") do |string|
    @email_options.subject = string
end

When("I set body to {string}") do |string|
    @email_options.body = string
end

When("I send email to created address") do
  @email_options.to = [@inbox.email_address]
  extra_instance.send_email(@inbox.id, @email_options)
end

Then("I can receive email") do
    opts = {
        'inbox_id': @inbox.id,
        'timeout': 10000
    }
    @received_email = api_instance.wait_for_latest_email(opts)
end

Then("I can see {string} in subject") do |string|
    expect(@received_email.subject).to include(string)
end

Then("I can see {string} in body") do |string|
    expect(@received_email.body).to include(string)
end