published on in elixir
tags: elixir plug controller testing

Testing a phoenix controller

The snippet below is a phoenix framework controller test

defmodule PaymentServiceWeb.Controllers.PaymentTest do
  use ExUnit.Case, async: false
  use Plug.Test
  alias PaymentServiceWeb.Router

  @tag endpoint: true
  test "service health check" do
    conn = conn(:get, "/api")
    response = Router.call(conn, @opts)
    assert %Plug.Conn{resp_body: body} = response
  end
  
end

Explanation

The test above is going to test an api endpoint, and the simple assertion is just to ensure that a %Plug.Conn struct is received as the response. conn/3 is used to issue a get request to the endpoint, where the 3rd parameter and be the request body in the case of a post request.