Skip to content

Instantly share code, notes, and snippets.

@jeremy-donson
Last active February 10, 2022 16:03
Show Gist options
  • Save jeremy-donson/e1fd3fa944c7f0037b6b2c16206103fb to your computer and use it in GitHub Desktop.
Save jeremy-donson/e1fd3fa944c7f0037b6b2c16206103fb to your computer and use it in GitHub Desktop.
TDD Elixir Phoenix Notes

TDD Elixir Phoenix Notes

Table of Contents

Chapter 1: Introduction

  • 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

  • Link To Page Top

Chapter 2: The What, the Why, and the How

  • Versions --< Releases --< Features --< Feature Tests --< Units -< Unit Tests
  • BDD is outside-in approach.
  • TDD is inside out approach.
  • Screen Shot 2022-01-27 at 9 30 10 AM

Chapter 3: Setting Up

  • 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

Chapter 3b: Leveraging Gitpod Environments

  • 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.

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

Chapter 13: Appendix: Troubleshooting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment