【问题标题】:Phoenix/Elixir test view helpersPhoenix/Elixir 测试视图助手
【发布时间】:2021-01-18 07:43:40
【问题描述】:

我创建了一个助手来在将数字呈现给视图之前进行一些计算:

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

谁能详细说明?

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    您正在测试文件中调用value_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_in_cents 不是二进制文件而失败吗?)

    【讨论】:

    • 感谢 Zwippie - 我删掉了很多其他功能,其他警卫基于类型:)
    猜你喜欢
    • 2011-04-02
    • 2016-03-10
    • 2015-04-22
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多