How to Build Maintainable Test Factories in Elixir and Phoenix

Photo by Patrick Hendry on Unsplash

What is a Factory?

How Do You Build Maintainable Factories?

Incidental Data vs. Intentional Data.

  1. Intentional data is necessary for your test to pass. Intentional data should be explicitly passed in, instead of being implicitly relied upon.
  2. Incidental data is not critical to the test. Your data structures expect a value, but the specific value does not matter. Incidental data can be implicit and randomized.

Explicitly Pass in Intentional Data.

Randomize Incidental Data.

Avoid Nesting Resources.

Centralize Data Creation in the Factory.

Separate Factories for Each Context

Separate Schema’s For Each Context

Options other than a factory

Test Fixtures

user = TestFixture.signUp()
TestFixture.deleteUser(user)
expectUnauthorized(getUserInfo(user))
user = Factory.insert(:user, %{state: "deleted"})
expectUnauthorized(getUserInfo(user))

Inserting from a data file.

Final Takeaways

--

--

--

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

Free Download Zwcad 2011 Crack

Another Wave, the Same Mistakes

Awesome PowerShell Tools and Tricks

AWS Serverless Services: Athena

HDP Cluster : Issues after namenode format

Using Fabric’s Crashlytics & Beta To Build Ideal Apps

A Warning About Sorting Dictionaries and Python Versions

Voyage: A Musical Adventure Traveling Through the Web

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

New Elixir Book: “100 Elixir Tips”

In Phoenix, how does the router, controller, template, and view code work together to render…

What is Elixir?

Elixir tap and then macros — life-saving helpers