Elixir basics · Lesson 17 (open contents)
Put one tested module in Mix
Read a Mix project layout and run a focused ExUnit test.
Run this first
Run it once, change one input, and compare the new result.
# Run these commands in the system terminal
mix new score_card
cd score_card
mix test- Mix creates
lib/,test/, andmix.exs; the generated test should pass.
Read from the first line down
- Read
mix new: Create a standard project tree. - Read
mix test: Compile the project and run ExUnit tests. - Read
assert left == right: Fail the test when both sides differ.
Symbols are not secret signs
mix newCreate a standard project tree.
mix testCompile the project and run ExUnit tests.
assert left == rightFail the test when both sides differ.
Match each name to its meaning
test file
ExUnit discovers files ending in _test.exs under the test directory.
assertion
assert states a result that must be true.
dependency
Dependencies are declared in mix.exs and fetched with Mix.
Why these forms are useful
Run the example first. Predict one result, then change one input and run it again.
ExUnit discovers files ending in _test.exs under the test directory.
assert states a result that must be true.
Close the answer and try
Write a test that expects ScoreCard.pass?(70) to be true.
test "70 passes" do
assert ScoreCard.pass?(70) == ____
endTarget result: The assertion should expect true.
Stuck? Read one hint
Replace the blank with the Boolean result.
After you run it, see one answer
test "70 passes" do
assert ScoreCard.pass?(70) == true
endThink about it: Where do normal source modules live?
Mix projects keep application source under lib/.
Remember these three lines
- 1
Mix gives a project a shared layout.
- 2
Tests describe observable behavior.
- 3
Keep early tests focused on pure functions.