Note: This project is currently in a work in progress so not all languages or sections are complete.

Test Driven Development with Elixir

Installing Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and run the following commands:

1
2
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Elixir

Now we can easily install Elixir with Homebrew using the following:

1
brew install elixir

Creating the Project

Now we'll use mix to initialize a project for us.

1
2
3
# mix is installed along with Elixir
mix new elixiray
cd elixiryay

Dependency Management

When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.

For Elixir we'll use the tool mix and our dependencies will be defined in the file mix.exs.

1
# mix is installed along with Elixir

Adding a Testing Library

We're going to use the testing library ExUnit to write our tests.

Edit the file mix.exs and add ExUnit as a testing dependency:

1

Then we retrieve ExUnit by running mix deps.get.

Finally we initialize ExUnit by running .

The First Test

Create the file text/elixiray_text.exs with the following contents:

1
2
3
4
5
6
7
8
defmodule ElixirayTest do
  use ExUnit.Case
  doctest Elixiray

  test "word starting with a vowel" do
    assert Elixiray.translate_word("apple") == "appleyay"
  end
end

Then run the test with the command mix test to see the results:

1
2
3
4
5
6
7
1) test word starting with a vowel (ElixirayTest)
   test/elixiray_test.exs:5
   ** (UndefinedFunctionError) function Elixiray.translate_word/1 is undefined or private
   code: assert Elixiray.translate_word("apple") == "appleyay"
   stacktrace:
     (elixiray) Elixiray.translate_word("apple")
     test/elixiray_test.exs:6: (test)

Comments