Testing the controller
Now that we have a way to define our controllers, it’s time to see how we can test them. Of course, we could simply test the entire HTTP request the same way we tested our HTTP servers in the previous chapters. However, there’s a better and simpler way to test the controller responses.
Since our routers and our controllers are plugs, we can test them using the Plug.Test
module. The Plug.Test
module contains a collection of helper functions that are designed to help us test plugs better.
Here is how we can use Plug.Test
to test our controller’s /
greet
action:
test/goldcrest/support/example_controller_test.exs
defmodule Goldcrest.ExampleControllerTest do use ExUnit.Case use Plug.Test describe "GET /greet" do test "responds with 200 status" do conn = conn(:get, "/greet") ...