Phoenix/Elixir测试视图帮助程序
我已经创建了一个助手,用于在向视图显示数字之前进行一些计算:Phoenix/Elixir测试视图帮助程序,elixir,phoenix-framework,Elixir,Phoenix Framework,我已经创建了一个助手,用于在向视图显示数字之前进行一些计算: defmodule FourtyWeb.CalculationHelpers do use Phoenix.HTML def value_in_euro(amount_in_cents) when is_binary(amount_in_cents) do cond do # some code end end end 这东西有用。我现在正在尝试测试它: defmodule Fourty
defmodule FourtyWeb.CalculationHelpers do
use Phoenix.HTML
def value_in_euro(amount_in_cents) when is_binary(amount_in_cents) do
cond do
# some code
end
end
end
这东西有用。我现在正在尝试测试它:
defmodule FourtyWeb.CalculationHelpersTest do
use FourtyWeb.ConnCase, async: true
import Phoenix.View
@amount_in_cents 1020
describe "it calculates the correct value in euro" do
test "with correct data" do
assert 10.20 == value_in_euro(@amount_in_cents)
end
end
end
如果我运行混合测试,会出现以下错误,我无法找出原因:
== Compilation error in file test/app_web/views/calculation_helpers_test.exs ==
** (CompileError) test/fourty_web/views/calculation_helpers_test.exs:11: undefined function value_in_euro/1
有人可以详细说明吗?您正在测试文件中调用
value\u in\u euro/1
,但该函数未定义。要解决此问题,请从定义它的模块调用它,如下所示:
assert 10.20 == FourtyWeb.CalculationHelpers.value_in_euro(@amount_in_cents)
或者导入模块,以便可以将FourtyWeb.CalculationHelpers
中的所有函数作为本地函数引用:
import FourtyWeb.CalculationHelpers
...
assert 10.20 == value_in_euro(@amount_in_cents) # now this works
(顺便说一句,在没有看到其余代码的情况下,该测试不会失败,因为
@amount\u in_cents
不是二进制的吗?您正在测试文件中调用value\u in_euro/1
,但该函数未定义。要解决此问题,请从定义它的模块调用它,如下所示:
assert 10.20 == FourtyWeb.CalculationHelpers.value_in_euro(@amount_in_cents)
或者导入模块,以便可以将FourtyWeb.CalculationHelpers
中的所有函数作为本地函数引用:
import FourtyWeb.CalculationHelpers
...
assert 10.20 == value_in_euro(@amount_in_cents) # now this works
(顺便说一句,在没有看到其余代码的情况下,这个测试不会失败吗,因为
@amount\u in_cents
不是二进制的?谢谢Zwippie-我删掉了很多其他函数,其他的防护基于类型:)谢谢Zwippie-我删掉了很多其他函数,其他防护基于类型:)