-
Erlang => Elixir => Phoenix
-
All notes below Derived From Here.
- It was helpful to learn about endpoints first from this article. My notes are here.
- Versions --< Releases --< Features --< Feature Tests --< Units -< Unit Tests
- Web-enabled gitpod environment per feature from this template.
- Chapter 1: Introduction
- Chapter 2: The What, the Why, and the How
- Chapter 3: Setting Up
- Chapter 3b: Leveraging Gitpod Environments
- Chapter 4: Showing a List of Chat Rooms
- Chapter 5: Refactoring Our Work
- Chapter 6: Creating Rooms
- Chapter 7: Putting the Chat in Chat Rooms
- Chapter 8: Authentication
- Chapter 9: Creating Users
- Chapter 10: Adding Authors to Chat
- Chapter 11: Adding History to Rooms
- Chapter 12: Conclusion
- Appendix: Troubleshooting
- Page Top Link
-
Test-driven is about our leveraging testing tools prior to coding, and to help guide our code quality.
-
TDD and BDD are both helpful ways to define testign specs.
-
Most good testing tools derive from the ruby language testing tools like rspec, cucumber, gherkin
- Versions --< Releases --< Features --< Feature Tests --< Units -< Unit Tests
- BDD is outside-in approach.
- TDD is inside out approach.
- Setup checkpoints:
- Install wallaby and other libraries. Test wallaby.
- This chatter app does not have supervisor. That is unfortunate, but next app will have GenServer and Supervisor.
- BBD => TDD => Larger Architectural Decisions ??
$ mix new chatter_sup --sup ; cd chatter_sup
$ mix test
$ TEST_FILE_PATH='test/chatter_sup_test.exs'
$ LINE_NUM=9
$ mix test ${TEST_FILE_PATH}:${LINE_NUM}
-
We will use wallaby for driving browser interaction testing.
- An alterative for this is hound.
-
Setup Steps:
-
Do we need sup option or other libraries like: jason + plug_cowboy
-
Add the following line to mix.exs:
{:wallaby, "~> 0.29.1", [runtime: false, only: :test]}
-
$ mix format
-
$ mix deps.get
-
Add the following lines to the end of test/test_helpers.exs:
{:ok, _} - Application.ensure_all_started(:wallaby)
Application.put_env(:wallaby, :base_url, ChatterWeb.Endpoint.url())
-
Make config/test.exs reflect the following:
-
import Config
config :chatter,
server: true
config :chatter, :sql_sandbox, true
import_config "#{config_env()}"
- Add this conditional to the top of lib/chatter_web/endpoint.ex
if Application.get_env(:chatter, :sql_sandbox) do
plug Phoenix.Ecto.SQL.Sandbox
end
-
We install ChromeDriver locally.
-
Troubleshooting may include wallaby and/or chomedriver.
-
Now we want to configure wallaby to use Chrome in config/test.exs:
config :wallaby, driver: Wallaby.Chrome
-
We should take a look at test/support/conn_case.ex
- This allows us to aggregate common code for feature and controller tests.
- We will be testing the view and the controllers. We would also like to test the model (later).
-
We will see things like:
defmodule ChatterWeb.SomeControllerTest do
use ChatterWeb.ConnCase, async: true
test "[testName]: This is a test description.", %{conn: conn} do # Use conn in test.
- Below we create and implement test/support/feature_case.ex
defmodule ChatterWeb.FeatureCase do
use ExUnit.CaseTemplate
using do
quote do
use Wallaby.DSL
alias ChatterWeb.Router.Helpers, as: Routes
@endpoint ChatterWeb.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Chatter.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.omde(Chatter.Repo, {:shared, self()})
end
metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(Chatter.Repo, self())
{:ok, session} = Wallaby.start_session(metadata: metadata)
{:ok, session: session}
end
end
- Now we can also do things like:
defmodule ChatterWeb.UserVisitsHomePage do
use ChatterWeb.FeatureCase, async: true
test "User visits homepage", %{session: session} do
session
|> visit("/")
|> assert_has(Query.css(".title", text: "Welcome to Chatter!"))
end
end
$ mix test test/chatter_web/features/user_visits_homepage_test.exs # Error: We expected 1, yet found 0.
# lib/chatter_web/router.ex
- get "/", PageController, :index
+ get "/", ChatRoomController, :index
- First we ensure that the above works and is clear.
- Then we port the working code above to gitpod, with excellent notes.
- Our first deployment target is heroku.
- We recommend reaching out to leverage the gitpod/heroku progress of others after working thru this doc.