檢視原始碼 ExUnit.Assertions (ExUnit v1.16.2)
此模組包含一組預設匯入至測試案例的斷言函式。
一般來說,開發人員會希望在測試中使用一般的 assert
巨集。此巨集會內省您的程式碼,並在發生失敗時提供良好的報告。例如,assert some_fun() == 10
會失敗(假設 some_fun()
傳回 13
)
Comparison (using ==) failed in:
code: assert some_fun() == 10
left: 13
right: 10
此模組也提供其他便利函式,例如 assert_in_delta
和 assert_raise
,以輕鬆處理其他常見情況,例如檢查浮點數或處理例外狀況。
摘要
函式
斷言其引數為真值。
斷言 值
為真值,否則顯示指定的 訊息
。
斷言 值1
和 值2
的差異不超過 增量
。
斷言 例外狀況
在 函式
執行期間引發。傳回救援的例外狀況,否則失敗。
斷言符合 pattern
的訊息在 timeout
週期內(以毫秒為單位)收到或將收到。
斷言符合 pattern
的訊息已收到且位於目前的處理程序郵件匣中。
斷言 expression
會導致錯誤。
斷言 expression
會結束。
斷言 expression
會擲回一個值。
傳回訊息並失敗。
負面斷言,預期表達式為 false
或 nil
。
斷言 value
為 nil
或 false
(亦即 value
不是真值)。
斷言 value1
和 value2
不在 delta
內。
斷言符合 pattern
的訊息在 timeout
週期內(以毫秒為單位)未收到(且不會收到)。
斷言符合 pattern
的訊息未收到(亦即它不在目前的處理程序郵件匣中)。
函式
斷言其引數為真值。
assert
內省基礎表達式,並在發生錯誤時提供良好的報告。例如,如果表達式使用比較運算子,訊息將顯示兩邊的值。斷言
assert 1 + 2 + 3 + 4 > 15
將會傳回訊息並失敗
Assertion with > failed
code: assert 1 + 2 + 3 + 4 > 15
left: 10
right: 15
類似地,如果給定比對表達式,它將根據該比對報告任何錯誤。給定
assert [1] = [2]
您將會看到
match (=) failed
code: assert [1] = [2]
left: [1]
right: [2]
請記住,assert
根據表達式不會改變其語意。換句話說,表達式仍需要傳回真值。例如,以下內容將會失敗
assert nil = some_function_that_returns_nil()
斷言 值
為真值,否則顯示指定的 訊息
。
範例
assert false, "it will never be true"
assert x == :foo, "expected x to be foo"
assert match?({:ok, _}, x), "expected x to match {:ok, _}"
斷言 值1
和 值2
的差異不超過 增量
。
此差異具有包容性,因此如果差異和 delta
相等,測試將會通過。
範例
assert_in_delta 1.1, 1.5, 0.2
assert_in_delta 10, 15, 2
assert_in_delta 10, 15, 5
斷言 例外狀況
在 函式
執行期間引發。傳回救援的例外狀況,否則失敗。
範例
assert_raise ArithmeticError, fn ->
1 + "test"
end
assert_raise RuntimeError, fn ->
raise "assertion will pass due to this raise"
end
斷言 例外狀況
在 函式
執行期間引發,並帶有預期的 訊息
,它可以是 Regex
或確切的 String
。傳回救援的例外狀況,否則失敗。
範例
assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
1 + "test"
end
assert_raise RuntimeError, ~r/^today's lucky number is 0\.\d+!$/, fn ->
raise "today's lucky number is #{:rand.uniform()}!"
end
斷言符合 pattern
的訊息在 timeout
週期內(以毫秒為單位)收到或將收到。
與 assert_received
不同,它有一個預設 timeout
為 100 毫秒。
pattern
參數必須是比對模式。如果未收到符合 pattern
的訊息,則會以 failure_message
失敗。
範例
assert_receive :hello
針對較長的逾時時間進行斷言
assert_receive :hello, 20_000
您也可以針對特定模式進行比對
assert_receive {:hello, _}
x = 5
assert_receive {:count, ^x}
斷言符合 pattern
的訊息已收到且位於目前的處理程序郵件匣中。
pattern
參數必須是比對模式。如果未收到符合 pattern
的訊息,則會以 failure_message
失敗。
逾時時間設定為 0,因此沒有等待時間。
範例
send(self(), :hello)
assert_received :hello
send(self(), :bye)
assert_received :hello, "Oh No!"
** (ExUnit.AssertionError) Oh No!
您也可以針對特定模式進行比對
send(self(), {:hello, "world"})
assert_received {:hello, _}
斷言 expression
會導致錯誤。
傳回錯誤或在其他情況下失敗。
範例
assert catch_error(error(1)) == 1
斷言 expression
會結束。
傳回目前程序的離開狀態/訊息或在其他情況下失敗。
範例
assert catch_exit(exit(1)) == 1
若要斷言從測試啟動的連結程序離開,請使用 Process.flag/2
攔截離開,並使用 assert_receive/2
斷言離開訊息。
Process.flag(:trap_exit, true)
pid = spawn_link(fn -> Process.exit(self(), :normal) end)
assert_receive {:EXIT, ^pid, :normal}
斷言 expression
會擲回一個值。
傳回拋出的值或在其他情況下失敗。
範例
assert catch_throw(throw(1)) == 1
傳回訊息並失敗。
範例
flunk("This should raise an error")
負面斷言,預期表達式為 false
或 nil
。
請記住,refute
不會變更給定表達式的語意。換句話說,下列程式碼將會失敗
refute {:ok, _} = some_function_that_returns_error_tuple()
上述程式碼會失敗,因為當兩邊不相等時,=
算子總是會失敗,而 refute/2
也不會變更它。
撰寫上述反駁的正確方式是使用 match?/2
refute match?({:ok, _}, some_function_that_returns_error_tuple())
範例
refute age < 0
斷言 value
為 nil
或 false
(亦即 value
不是真值)。
範例
refute true, "This will obviously fail"
斷言 value1
和 value2
不在 delta
內。
此差異是獨佔的,因此如果差異和 delta 相等,測試將會失敗。
如果您提供 message
,值相關的資訊將自動附加到其中。
範例
refute_in_delta 1.1, 1.2, 0.2
refute_in_delta 10, 11, 2
斷言符合 pattern
的訊息在 timeout
週期內(以毫秒為單位)未收到(且不會收到)。
pattern
參數必須是比對樣式。如果收到與 pattern
相符的訊息,則會以 failure_message
失敗。
範例
refute_receive :bye
以明確的逾時時間反駁接收
refute_receive :bye, 1000
斷言符合 pattern
的訊息未收到(亦即它不在目前的處理程序郵件匣中)。
pattern
參數必須是比對樣式。如果收到與 pattern
相符的訊息,則會以 failure_message
失敗。
逾時時間設定為 0,因此沒有等待時間。
範例
send(self(), :hello)
refute_received :bye
send(self(), :hello)
refute_received :hello, "Oh No!"
** (ExUnit.AssertionError) Oh No!