What to look for, to do TDD,
in a language you're not familiar with.
End user, in prod
Automated test, in dev
# test_helper.exs
ExUnit.start()
# my_super_module_test.exs
defmodule MySuperModuleTest do
use ExUnit.Case
test "add two numbers" do
result = MySuperModule.add 1, 2
assert result == 3
end
end
mix test
🎁 Because TDD is awesome
# mix.exs (Elixir 1.4)
def deps do
[{:mix_test_watch, "~> 0.5", only: :dev, runtime: false}]
end
# mix.exs (Elixir 1.3 and earlier)
def deps do
[{:mix_test_watch, "~> 0.5", only: :dev}]
end
mix test.watch
Credits Stefano Alletti
Hexagonal Architecture
Abstract
Domain-API
Interface
Concrete
Infrastructure
Implementation
Inject a custom one to test
# Domain
module PoetryReader do
@poetry_library Application.get_env(:poetry_library)
def give_me_some_poetry do
@poetry_library.get_a_poem()
|> do_some_other_transformation
end
end
module PoetryLibrary do
@callback get_a_poem() :: {:ok, String.t()}
end
# Infrastructure
module PoetryLibrary.Http do
@behaviour MyApp.PoetryLibrary
def get_a_poem() do
url = "https://some-domain.com/poems/random"
headers = [{"Accept", "application/json"}]
HTTPoison.get!(url, headers)
|> Map.get(:body)
|> decode_response_into_string
end
end
# Application setup
config :poetry_library, PoetryLibrary.Http
Mocks and explicit contracts in Elixir
def deps do
[{:mox, "~> 0.3", only: :test}]
end
# test/test_helper.ex
Mox.defmock(PoetryLibrary.Mock, for: PoetryLibrary)
Application.put_env(:poetry_library, PoetryLibrary.Mock)
# As a stub
PoetryLibrary.Mock
|> stub(:get_a_poem, fn() -> {:ok, "Some verses for you"} end)
# As a mock
PoetryLibrary.Mock
|> expect(:get_a_poem, fn() -> {:ok, "Some verses for you"} end)
mix test.watch --stale
📚 Ensure your docs examples are up-to-date
defmodule MySuperModule do
@doc """
Return a list of things.
## Examples
iex> MySuperModule.list()
[1, 4, true, 3]
"""
def list(), do: [1, 4, true, 3]
end
defmodule MySuperModuleTest do
use ExUnit.Case
doctest MySuperModule
# …
end
If you're going to write crappy code
in a new language…
Do TDD, so you can refactor later!
@nicoespeon
Web developer at Busbud 🚎
#Crafter #Meetup #FRP #Agile