ExUnit Testing in Elixir

What is the best way to learn Elixir ExUnit Testing. As I am quite new to Elixir. So even after seeing the docs and auto generated tests. I am unable to create test for my code

  def show(conn, %{"id" => exid}) do
    current_user = conn.assigns[:current_user]
    camera = Camera.by_exid_with_associations(exid)

    with :ok <- ensure_camera_exists(camera, exid, conn),
         :ok <- ensure_can_edit(current_user, camera, conn),
         do: camera.cloud_recordings |> render_cloud_recording(conn)
 end
4 Likes

For Phoenix you should be looking at Phoenix related ExUnit testing guides. I personally haven’t seen guides online but I’ve read the Programming Phoenix book and can totally recommend it for Phoenix in general and to learn how to test Phoenix applications.

5 Likes

Welcome to the forums!

I’ll second @sashaafm’s recommendation for the Programming Phoenix book, as it has some really helpful tutorials on ExUnit testing.

My perception of Phoenix ExUnit controller testing (and please others interject if I’m missing something), has been that at at high-level looks like:

  1. Create your test connection which transforms your setup conn.
    conn = get conn, "/"
  2. Assert something about that conn; An expected response, content-type, or response body for example.
    assert html_response(conn, 200) =~"...Hello"

Lastly, if you narrowed the scope of your question with some details on the test you attempted for that show action, we might be able to more easily help bridge whatever gaps you’re missing in how to formulate tests.

Cheers

7 Likes

@david what you said is correct.

However, there are many more intricacies to Phoenix testing like asserting the Ecto validations on Models are working correctly, asserting the Views are being rendered properly and with the proper data, asserting connection related functions (like being able to get into a page even if logged out, restricting certain users from pages they do not belong).

6 Likes

This official guide will go through making an entire application in a test driven manner with ExUnit. Even has doc testing as well.

5 Likes

Thanks everybody for your replies. I sorted out by seeing some other tests.

Testing in Phoenix/Elixir is so far the best as compared to Rails. It never gets to me in Rails but in Elixir its awesome…