mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Fix and refactor game controller tests
This commit is contained in:
parent
8634d7aa9f
commit
070f9b1474
@ -4,11 +4,17 @@ defmodule Chess.GameControllerTest do
|
|||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
alias Chess.Auth.Guardian
|
alias Chess.Auth.Guardian
|
||||||
|
|
||||||
import Chess.Factory, only: [create_user: 0, create_user: 2]
|
import Chess.Factory,
|
||||||
|
only: [create_user: 0, create_user: 2, create_game_for: 2]
|
||||||
|
|
||||||
test "lists all entries on index", %{conn: conn} do
|
test "lists all entries on index", %{conn: conn} do
|
||||||
conn = login(conn)
|
user = create_user()
|
||||||
conn = get conn, game_path(conn, :index)
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> login(user)
|
||||||
|
|> get(game_path(conn, :index))
|
||||||
|
|
||||||
assert html_response(conn, 200) =~ "Listing games"
|
assert html_response(conn, 200) =~ "Listing games"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -16,21 +22,35 @@ defmodule Chess.GameControllerTest do
|
|||||||
opponent = create_user("daruk", "deathmountain")
|
opponent = create_user("daruk", "deathmountain")
|
||||||
attrs = %{"opponent_id" => opponent.id}
|
attrs = %{"opponent_id" => opponent.id}
|
||||||
|
|
||||||
conn = login(conn)
|
user = create_user()
|
||||||
conn = post conn, game_path(conn, :create), game: attrs
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> login(user)
|
||||||
|
|> post(game_path(conn, :create), game: attrs)
|
||||||
|
|
||||||
game = Repo.one(Game)
|
game = Repo.one(Game)
|
||||||
|
|
||||||
assert redirected_to(conn) == game_path(conn, :show, game)
|
assert redirected_to(conn) == game_path(conn, :show, game)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "shows chosen resource", %{conn: conn} do
|
test "shows chosen resource", %{conn: conn} do
|
||||||
conn = login(conn)
|
user = create_user()
|
||||||
game = Repo.insert! %Game{}
|
opponent = create_user("revali", "vahmedoh")
|
||||||
conn = get conn, game_path(conn, :show, game)
|
game = create_game_for(user, opponent)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> login(user)
|
||||||
|
|> get(game_path(conn, :show, game))
|
||||||
|
|
||||||
assert html_response(conn, 200) =~ "<div id=\"app\" data-game-id=\"#{game.id}\">"
|
assert html_response(conn, 200) =~ "<div id=\"app\" data-game-id=\"#{game.id}\">"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders page not found when id is nonexistent", %{conn: conn} do
|
test "renders page not found when id is nonexistent", %{conn: conn} do
|
||||||
conn = login(conn)
|
user = create_user()
|
||||||
|
conn = login(conn, user)
|
||||||
|
|
||||||
assert_error_sent 404, fn ->
|
assert_error_sent 404, fn ->
|
||||||
get conn, game_path(conn, :show, -1)
|
get conn, game_path(conn, :show, -1)
|
||||||
end
|
end
|
||||||
@ -38,14 +58,15 @@ defmodule Chess.GameControllerTest do
|
|||||||
|
|
||||||
test "deletes chosen resource", %{conn: conn} do
|
test "deletes chosen resource", %{conn: conn} do
|
||||||
game = Repo.insert! %Game{}
|
game = Repo.insert! %Game{}
|
||||||
conn = login(conn)
|
user = create_user()
|
||||||
|
conn = login(conn, user)
|
||||||
conn = delete conn, game_path(conn, :delete, game)
|
conn = delete conn, game_path(conn, :delete, game)
|
||||||
assert redirected_to(conn) == game_path(conn, :index)
|
assert redirected_to(conn) == game_path(conn, :index)
|
||||||
refute Repo.get(Game, game.id)
|
refute Repo.get(Game, game.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp login(conn) do
|
defp login(conn, user) do
|
||||||
user = create_user()
|
conn
|
||||||
conn |> Guardian.Plug.sign_in(user)
|
|> Guardian.Plug.sign_in(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user