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
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.
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:
Create your test connection which transforms your setup conn. conn = get conn, "/"
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.
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).