Alex Antonov

Software craftsman


book reviews (5)

javascript (4)

elixir (3)

ruby (2)

react.js (2)

python (1)

How do I test sessions in ExUnit?

in elixir

This is the small recipe I want to share with community. I had a troubles with testing sessions in ExUnit. I started to google for answer. Only big piece of hard to understand code in Testing Phoenix controllers article by Alex Marandon. But actually, this piece of code didn’t worked, at least to me.

I also found github issue in phoenix repo. Chris McCord said there are two ways to test sessions:

Actually code is simple, firstly, I wrote a module in test support. It’s about returning conn() with a session assigned:

# test/support/signed_conn.ex
defmodule MyApp.SignedConn do
  @endpoint MyApp.Endpoint

  import Phoenix.ConnTest

  def signed_user_conn do
    user = MyApp.Factory.create(:user)

    |> post("/your/new_session_path", %{
      "session" => %{
        "email"    =>,
        "password" => user.password

As you can notice, I created a user via ex_machina and then made a post request to my sessions controller.

Now, let’s import MyApp.SignedConn to MyApp.ConnCase and

Volia! We can use conn() without any session assigned, but signed_user_conn() for connections with already assigned user session!