Skip to content

Commit

Permalink
Use Phoenix testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Mar 16, 2020
1 parent 0eb7517 commit 5c93e2b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 27 deletions.
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ config :maru, MongoosePush.Router,

config :mongoose_push, MongoosePushWeb.Endpoint,
http: [port: 8445],
server: true,
server: false,
check_origin: false,
watchers: []

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ defmodule MongoosePush.Mixfile do

defp test_paths(:integration), do: ["test/integration", "test/common"]

defp test_paths(_), do: ["test/unit", "test/common"]
defp test_paths(_), do: ["test/unit", "test/common", "test/web"]
end
34 changes: 34 additions & 0 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule MongoosePushWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use MpushDummyWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate

using do
quote do
# Import conveniences for testing with connections
use Phoenix.ConnTest
alias MongoosePushWeb.Router.Helpers, as: Routes

# The default endpoint for testing
@endpoint MongoosePushWeb.Endpoint
end
end

setup _tags do
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
25 changes: 0 additions & 25 deletions test/unit/phx_simple_handler_test.exs

This file was deleted.

19 changes: 19 additions & 0 deletions test/web/controllers/api_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule MongoosePushWeb.ApiControllerTest do
use MongoosePushWeb.ConnCase
alias MongoosePushWeb.Router.Helpers, as: Routes

setup %{conn: conn} do
TestHelper.reload_app()
conn = conn
|> put_req_header("accept", "application/json")
|> put_req_header("content-type", "application/json")
{:ok, conn: conn}
end

test "POST /simple", %{conn: conn} do
body = %{service: :fcm, body: "body", title: "title"}
json = Poison.encode!(body)
conn = post(conn, "/simple", json)
json_response(conn, 200)
end
end
9 changes: 9 additions & 0 deletions test/web/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ExUnit.start()

defmodule TestHelper do
def reload_app() do
Application.stop(:mongoose_push)
{:ok, _} = Application.ensure_all_started(:mongoose_push)
:ok
end
end

0 comments on commit 5c93e2b

Please sign in to comment.