【问题标题】:Test method delegation in ElixirElixir 中的测试方法委托
【发布时间】:2017-07-09 13:41:27
【问题描述】:

我有两个模块:

defmodule Base.ModuleOne do
  def do_stuff(opts) do
    "some stuff with opts"
  end
end

defmodule Base.ModuleTwo do
  defdelegate do_stuff(opts), to: Base.ModuleOne
end

在不同命名空间下对Base.ModuleOne.do_stuff/1 函数进行复制粘贴测试时,测试委派的正确方法是什么?

【问题讨论】:

    标签: testing elixir delegation ex-unit


    【解决方案1】:

    在撰写此答案时,我还没有找到测试defdelegate 的“官方”方法。在您的情况下,我能想到的唯一选择是:

    1. 只需忽略 Base.ModuleTwo.do_stuff(opts) 的测试,假设您的基础已包含在 Base.ModuleOne.do_stuff(opts) 的测试中。
    2. Base.ModuleOne.do_stuff(opts) 中的大部分(如果不是全部)测试移动到Base.ModuleTwo.do_stuff(opts) 的测试中,这样您的测试就可以直接与模块的公共接口相关,从而将Base.ModuleOne 降级为“私有”实施细节。
    3. 由于 mocking would seem to be generally out of favour in Elixir,请考虑将 defdelegate 替换为依赖注入(使用函数或模块;有关详细信息,请参阅上一个链接),这样您就可以通过以下方式编写测试:

    模块:

    defmodule Base.ModuleTwo do
      def do_stuff(opts, dependency \\ Base.ModuleOne)
        dependency.do_stuff(opts)
      end
    end
    

    测试:

    defmodule ModuleTwoTest do
      use ExUnit.Case
    
      describe "Base.ModuleTwo.do_stuff/2" do
        defmodule TestDependency do
          def do_stuff(_opts) do
            send self(), :do_stuff
          end
        end
    
        test "calls do_stuff/1 on its dependency" do
          opts = %{foo: "bar"} # or whatever the `opts` should be
          Base.ModuleTwo.do_stuff(opts, TestDependency)
          assert_received :do_stuff
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2016-08-21
      • 2014-07-21
      • 2020-06-20
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      相关资源
      最近更新 更多