Skip to content

Instantly share code, notes, and snippets.

@Kjir
Last active January 7, 2017 17:12
Show Gist options
  • Save Kjir/9f963b58d802d609e26550af5a03f34d to your computer and use it in GitHub Desktop.
Save Kjir/9f963b58d802d609e26550af5a03f34d to your computer and use it in GitHub Desktop.
First Phoenix API test
defmodule Quotes.SubscriberControllerTest do
use Quotes.ConnCase
import Quotes.Factory
test "#index renders a list of subscribers" do
conn = build_conn()
subscriber = insert(:subscriber)
conn = get conn, subscriber_path(conn, :index)
assert json_response(conn, 200) == %{
"subscribers" => [%{
"first_name" => subscriber.first_name,
"last_name" => subscriber.last_name,
"inserted_at" => DateTime.to_iso8601(subscriber.inserted_at), # Same error if I use Ecto.DateTime here
"updated_at" => subscriber.updated_at
}]
}
end
describe "#create" do
test "renders the created subscriber" do
conn = build_conn()
subscriber = %{
first_name: "Harry",
last_name: "Potter"
}
conn = post conn, subscriber_path(conn, :create, subscriber: subscriber)
assert json_response(conn, 201) == %{
"subscriber" => %{
"first_name" => subscriber.first_name,
"last_name" => subscriber.last_name
}
}
end
end
end
mix test
...
1) test #index renders a list of subscribers (Quotes.SubscriberControllerTest)
test/controllers/subscriber_controller_test.exs:5
** (FunctionClauseError) no function clause matching in DateTime.to_iso8601/1
stacktrace:
(elixir) lib/calendar.ex:1642: DateTime.to_iso8601(~N[2017-01-07 15:12:12.257267])
test/controllers/subscriber_controller_test.exs:15: (test)
.
Finished in 0.1 seconds
5 tests, 1 failure
Randomized with seed 182402
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment