我正在尝试做这样的事情,不必手动编写一系列test
块:
test_cases = %{
"foo" => 1,
"bar" => 2,
"baz" => 3,
}
Enum.each(test_cases, fn({input, expected_output}) ->
test "for #{input}" do
assert(Mymodule.myfunction input) == expected_output
end
end)
Run Code Online (Sandbox Code Playgroud)
但是,在运行此代码时,我得到的错误undefined function input/0
就行了assert(Mymodule.myfunction input) == expected_output
.
有没有办法实现我想要的?
是的,它是可能的,你只需要unquote
两个input
和expected_output
里面do
你传递给块test/2
.
test_cases = %{
"foo" => 1,
"bar" => 2,
"baz" => 3,
}
Enum.each test_cases, fn({input, expected_output}) ->
test "for #{input}" do
assert Mymodule.myfunction(unquote(input)) == unquote(expected_output)
end
end
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你在assert
调用assert/1
时遇到了一个parens错误Mymodule.myfunction input
,而不是Mymodule.myfunction(input) == expected_output
(这是你试图断言的表达式).
归档时间: |
|
查看次数: |
351 次 |
最近记录: |