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

A roundtrip on Modern Data Platform with Azure Synapse Analytics and Azure DevOps — Part One

One common solution to ingest, analyse and store data

“No Regrets” — Read what Louis Kirkham had to say about Makers

Adding Paypal payments to your Ionic 2 application

A week with… Guillermo Snipe (and the Backbase Academy Team)

Configuring CI/CD on Kubernetes with Jenkins

How to Git?

How to check Github Actions Usage?

Git Incorrect Upstream Branch

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

Building a simple Calendly clone with Phoenix LiveView (pt. 4)

Hot Code Reloading of Elixir OTP Application

Implement Sigil in Elixir

A Strange pitfall with Map.get & its default behavior | Elixir Basics