-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from erickmp07/fifth-class
Fifth class
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule Inmana.RestaurantTest do | ||
use Inmana.DataCase, async: true | ||
|
||
alias Ecto.Changeset | ||
alias Inmana.Restaurant | ||
|
||
describe "changeset/1" do | ||
test "when all params are valid, returns a valid changeset" do | ||
params = %{name: "Restaurant name", email: "[email protected]"} | ||
|
||
response = Restaurant.changeset(params) | ||
|
||
assert %Changeset{ | ||
changes: %{ | ||
email: "[email protected]", | ||
name: "Restaurant name" | ||
}, | ||
valid?: true | ||
} = response | ||
end | ||
|
||
test "when there are invalid params, returns an invalid changeset" do | ||
params = %{name: "R", email: ""} | ||
expected_response = %{ | ||
email: ["can't be blank"], | ||
name: ["should be at least 2 character(s)"] | ||
} | ||
|
||
response = Restaurant.changeset(params) | ||
|
||
assert %Changeset{valid?: false} = response | ||
|
||
assert errors_on(response) == expected_response | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
test/inmana_web/controllers/restaurants_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule InmanaWeb.RestaurantsControllerTest do | ||
use InmanaWeb.ConnCase, async: true | ||
|
||
describe "create/2" do | ||
test "when all params are valid, creates the restaurant", %{conn: connection} do | ||
params = %{name: "Restaurant name", email: "[email protected]"} | ||
|
||
response = | ||
connection | ||
|> post(Routes.restaurants_path(connection, :create, params)) | ||
|> json_response(:created) | ||
|
||
assert %{ | ||
"message" => "Restaurant created!", | ||
"restaurant" => %{ | ||
"email" => "[email protected]", | ||
"id" => _id, | ||
"name" => "Restaurant name" | ||
} | ||
} = response | ||
end | ||
|
||
test "when there are invalid params, returns an error", %{conn: connection} do | ||
params = %{email: "[email protected]"} | ||
expected_response = %{"message" => %{"name" => ["can't be blank"]}} | ||
|
||
response = | ||
connection | ||
|> post(Routes.restaurants_path(connection, :create, params)) | ||
|> json_response(:bad_request) | ||
|
||
assert response == expected_response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule InmanaWeb.RestaurantsViewTest do | ||
use InmanaWeb.ConnCase, async: true | ||
|
||
import Phoenix.View | ||
|
||
alias Inmana.Restaurant | ||
alias InmanaWeb.RestaurantsView | ||
|
||
describe "render/2" do | ||
test "renders create.json" do | ||
params = %{name: "Restaurant name", email: "[email protected]"} | ||
{:ok, restaurant} = Inmana.create_restaurant(params) | ||
|
||
response = render(RestaurantsView, "create.json", restaurant: restaurant) | ||
|
||
assert %{ | ||
message: "Restaurant created!", | ||
restaurant: %Restaurant{ | ||
email: "[email protected]", | ||
id: _id, | ||
name: "Restaurant name" | ||
} | ||
} = response | ||
end | ||
end | ||
end |