Elixir | ExUnit.Assersions #elixir

# Elixir | ExUnit.Assersions #elixir
## 概要
Elixir の ExUnit.Assersions について。
## サンプルコード
~~~elixir
defmodule SampleExunitTest do
  use ExUnit.Case

  # 真偽検証
  test "assert(assertion) success", do: assert true
  test "assert(assertion) fail", do: assert false

  # メッセージ付き真偽検証
  test "assert(value, message) success", do: assert(true, "hoge")
  test "assert(value, message) fail", do: assert(false, "hoge")

  # エラー検証
  test "assert_raise(exception, function) success" do
    assert_raise ArithmeticError, fn ->
      0 / 0
    end
  end
  test "assert_raise(exception, function) fail" do
    assert_raise ArithmeticError, fn ->
      1 / 1
    end
  end

  #メッセージ付きエラー検証
  test "assert_raise(exception, message, function) success" do
    assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
      0 / 0
    end
  end
  test "assert_raise(exception, message, function) fail" do
    assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
      1 / 1
    end
  end

  # 強制エラー
  test "flunk(message \\ \"Flunked!\") default", do: flunk
  test "flunk(message \\ \"Flunked!\") with message", do: flunk("flunk fail")

  # エラー番号
  test "catch_exit(expression) success", do: assert catch_exit(exit 1) == 1
  test "catch_exit(expression) fail", do: assert catch_exit(true) == 1

  # assert の反対( true 時に失敗になる)
  test "refute(assertion) success", do: refute false
  test "refute(assertion) fail", do: refute true
end
~~~
## 実行結果

~~~
$ mix test --trace

SampleExunitTest
  * refute(assertion) fail (6.1ms)

  1) test refute(assertion) fail (SampleExunitTest)
     test/sample_exunit_test.exs:46
     Expected false or nil, got true
     code: true
     stacktrace:
       test/sample_exunit_test.exs:46

  * flunk(message \ "Flunked!") with message (0.01ms)

  2) test flunk(message \ "Flunked!") with message (SampleExunitTest)
     test/sample_exunit_test.exs:38
     flunk fail
     stacktrace:
       test/sample_exunit_test.exs:38

  * assert(value, message) fail (0.01ms)

  3) test assert(value, message) fail (SampleExunitTest)
     test/sample_exunit_test.exs:10
     hoge
     stacktrace:
       test/sample_exunit_test.exs:10

  * assert(value, message) success (0.00ms)
  * assert_raise(exception, function) fail (1.6ms)

  4) test assert_raise(exception, function) fail (SampleExunitTest)
     test/sample_exunit_test.exs:18
     Expected exception ArithmeticError but nothing was raised
     stacktrace:
       test/sample_exunit_test.exs:19

  * assert_raise(exception, message, function) success (6.6ms)
  * catch_exit(expression) fail (3.1ms)

  5) test catch_exit(expression) fail (SampleExunitTest)
     test/sample_exunit_test.exs:42
     Expected to catch exit, got nothing
     stacktrace:
       test/sample_exunit_test.exs:42

  * assert_raise(exception, message, function) fail (1.0ms)

  6) test assert_raise(exception, message, function) fail (SampleExunitTest)
     test/sample_exunit_test.exs:30
     Expected exception ArithmeticError but nothing was raised
     stacktrace:
       test/sample_exunit_test.exs:31

  * assert(assertion) fail (0.01ms)

  7) test assert(assertion) fail (SampleExunitTest)
     test/sample_exunit_test.exs:6
     Expected truthy, got false
     code: false
     stacktrace:
       test/sample_exunit_test.exs:6

  * catch_exit(expression) success (0.00ms)
  * flunk(message \ "Flunked!") default (0.01ms)

  8) test flunk(message \ "Flunked!") default (SampleExunitTest)
     test/sample_exunit_test.exs:37
     Flunked!
     stacktrace:
       test/sample_exunit_test.exs:37

  * assert_raise(exception, function) success (0.01ms)
  * assert(assertion) success (0.00ms)
  * refute(assertion) success (0.00ms)


Finished in 0.1 seconds (0.1s on load, 0.02s on tests)
14 tests, 8 failures

Randomized with seed 77820
~~~

## 参照
[ExUnit.Assertions](http://elixir-lang.org/docs/master/ex_unit/ExUnit.Assertions.html)