marick

marick

Decoupling client code and tests from complex structures

I have written a description of one way to allow complex structures to change without breaking a lot of client code and tests. It describes the use of one package you can add to Mix dependencies, plus two modules you can copy and tweak. I show the beginning of the writeup below. The whole thing is at Stubbing Complex Structures. Around 300 lines, including code samples.


Sometimes, despite what you might want, you end up with a complex structure – sometimes called a “God object” – that’s used in many places in your code. If that client code contains text like user.privileges[:author].read, you have two problems:

  1. Any attempt to change the “shape” of the complex structure becomes hard because there are so many places to change. That locks you into complexity because it’s too painful to undo it by, for example, breaking the single structure into several.

  2. Tests have to construct sample data. In a dynamically typed language, they don’t have to create a complete God object; they need only supply the fields the code under test actually uses. But, once again, changes to the structure can require a lot of changes to tests.

Here, I’ll show how to avoid such coupling, using the code in MockeryExtras.Getters and MockeryExtras.Given. The emphasis is on both simplifying change and avoiding busywork.

Look in the example directory for working code to adapt.

TL;DR

Make getters

defmodule Example.RunningExample do
  import MockeryExtras.Getters
  
  defstruct [:example, :history]

  getters :example, [
    eens: [], field_checks: %{}
  ]
  getters :example, :metadata, [
    :name, :workflow_name, :repo, :module_under_test
  ]

Use getters - tersely and stubbably - in client code:

defmodule Example.Steps do
  use Example.From

  def assert_valid_changeset(running, which_changeset) do 
    from(running, use: [:name, :workflow_name])                 # <<<<
    from_history(running, changeset: which_changeset)           # <<<<
    # `name = RunningExample.name(running)`, etc. is too wordy

    do_something_with(name, workflow_name, changeset)
  end

The above requires copying and slightly tweaking code in Example.From.

Don’t build structures, stub getters

defmodule Example.StepsTest do
  ... 
  import Example.RunningStubs

  setup do # overridable defaults
    stub [name: :example, workflow_name: :success]
    :ok
  end

  test "..." do 
    stub(field_checks: %{name: "Bossie"}, ...)
    stub_history(inserted_value: ...)
    ...
    assert ...
end

The above requires copying and slightly tweaking code in Example.RunningStubs.

Where Next?

Popular in Guides/Tuts Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
AstonJ
This blog post hit my timeline earlier, and I’ve also been learning about some fantastic Elixir related tips via @pragdave’s new online c...
New
ben-pr-p
https://github.com/ben-pr-p/elixir-phoenix-parcel-example Hey all! I put together a starter-pack / instructions to set up Phoenix with t...
New
annad
I’m posting this for developers who are totally new to Phoenix like myself. This is all probably obvious to more skilled Phoenix develope...
New
sergio
https://sergiotapia.me/generate-images-with-name-initials-using-elixir-and-imagemagick-374eca4d14ff Hope this saves you guys some time!...
New
tonydang
I recently got inertia-phoenix (an Inertia.js adapter for Phoenix) working with Svelte. Setting up the server-side rendering (SSR) with S...
New
f0rest8
Hi, TLDR: form attribute set on the input fields and button submit. I just wanted to share a solution I discovered when making live inl...
New
Eiji
Hey, today I give amnesia library a try and found a few problems. I would like describe how to setup it properly and solve problems which...
New
niku
I have published an elixir project with using Travis CI. I would like to share some tips &amp; thoughts that I was getting through this ...
New
caspg
Hi everyone, I recently implemented a real-time search feature in a Phoenix application using LiveView and Tailwind, and I wanted to sha...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement