Mocks in Elixir/Phoenix using Behaviors and Environment variables.

Mocks are the squirt gun vs. the real gun. They can mimic behavior but aren’t nearly as dangerous.

The Goal

  • Introduces flakiness and a reliance on network connectivity
  • Slows down tests that need to wait for the response
  • Avoid coupling code directly to the library in case we change libraries.
  • Working directly with a library that can send real SMS text messages is risky.

The Design

Access and Set Environment Variables.

To Set An Environment Variable.

  • :app_name should be your applications name
  • sms_service is the env variable name. It can be any valid name you want.
  • App.Sms.MockSms is the SMS module you want to use for that environment. In dev.exs and test.exs, it should be MockSms, and in prod.exs it should be ProdSms

To Retrieve an Environment Variable.

Create The Prod and Mock Modules.

Delegate to the Prod and Mock Modules.

Test the Prod Module.

Final Thoughts.

--

--

--

Software Engineer. I create educational content focused on technology for mobile and web applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The First Things About Your Teams’ Agility

Know your Azure environment better with Grafana

Idiot-Proof Guide to 301 Redirects

Introducing $FUEL for Vehixels

Should a Product Owner in a corporate bypass stakeholders to move fast?

While there are a multitude of different types of hackers, the database is the one goal they all…

Flatiron Sinatra Project

The Art of Spark Kernel Design

Image result for spark logo

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brooklin Myers

Brooklin Myers

Software Engineer. I create educational content focused on technology for mobile and web applications.

More from Medium

Using Ecto changesets for JSON API request body validation.

An opinionated Elixir Style Guide

The beauty of OTP

Elixir tap and then macros — life-saving helpers