Jake Gordon

Welcome, I'm Jake Gordon, a full stack developer who builds web and mobile applications for SaaS companies, and occasionally I make fun games on the side. I am based in Portland, Oregon and Seattle, Washington.

Latest Article

Testing Dates in Elixir

Wed, Aug 19, 2020

I recently worked on an Elixir/Phoenix project with some complex date processing logic and needed to test the behaviour in a simple and predictable way. Unfortunately any code that directly uses DateTime.utc_now will generate different results every time we run the test suite and make it hard to assert the correct results.

For example, consider the following trivial example:

defmodule Event
  defstruct [ :type, :occurred_on ]
  def new(type) do
    %Event{ type: type, occurred_on: DateTime.utc_now }
test "event occurs now" do
  event = Event.new(:signup)
  assert event.name == :signup
  assert event.occurred_on == DateTime.utc_now     # BANG!

This test fails because time progressed between Event.new and the final assert.