Testing
Maru.Test
only works with designated Maru.Router
, so if you want to test a router named API
, you should use Maru.Test
like this:
defmodule API do
use Maru.Router
get do
text(conn, "hello")
end
end
defmodule APITest do
use Maru.Test, for: API
test "/" do
assert "hello" = get("/") |> text_response
end
end
Test router include multiple versions
defmodule API do
use Maru.Router
version "v1" do
get do
text(conn, "hello")
end
end
version "v2" do
get do
json(conn, %{hello: :world}
end
end
end
defmodule APITest do
use Maru.Test, for: API
test "/ v1" do
assert "hello" = get("/", "v1") |> text_response
end
test "/ v2" do
assert %{"hello" => "world"} = get("/", "v2") |> json_response
end
end
Test router with headers or params
defmodule API do
use Maru.Router
params do
requires :foo
end
post :path do
...
end
end
defmodule APITest do
use ExUnit.Case
use Maru.Test, for: API
setup do
conn =
build_conn()
|> put_req_header("content-type", "application/json")
|> put_plug(Plug.Parsers, parsers: [:json], pass: ["*/*"], json_decoder: Poison)
{:ok, %{conn: conn}}
end
test "/", %{conn: conn} do
assert %Plug.Conn{} = conn |> put_body_or_params(~s({"foo":"bar"})) |> post("/path")
end
end
Updated less than a minute ago