【问题标题】:Mocking consecutive function calls in Elixir with Mock or Mox使用 Mock 或 Mox 在 Elixir 中模拟连续的函数调用
【发布时间】:2022-01-26 12:31:46
【问题描述】:

我正在尝试模拟函数多次调用,因此它每次都会返回特定的不同值。我对 Elixir 和函数概念不太熟悉。

defmodule Roller do
  def roll do
      1..10
      |>Enum.random()
  end
end

Roller 每次调用都会返回一个随机数。

defmodule VolunteerFinder do
  import Roller

  def find(list) do
    find(list, []);
  end

  defp find([] = _list, result) do
    result
  end

  defp find([head | tail] = _list, result) do
    result = [%{name: head, score: Roller.roll()} | result]
    find(tail, result)
  end
end

所以假设list 包含多个元素,则调用roller 2 次。在我的测试中,我需要以某种方式控制它。

我用 Mock 试过了。我想以最简单的方式做这样的事情。不必在任何地方保存一些状态或为每次调用运行单独的进程,这将是很棒的。我知道 Elixir 的思维方式可能与我的客观范式思维方式有所不同。测试VolunteerFinder 模块的最正确 Elixir 方法是什么?

defmodule VolunteerFinderTest do
  use ExUnit.Case

  import Mock
  import Roller

  test_with_mock(
    "Find volunteer for list with one element",
    Roller,
    [roll: fn() -> 5 end]
  ) do
    assert VolunteerFinder.find(["John"]) == [%{name: "John", score: 5}]
  end


  test_with_mock(
    "Find volunteer for list with two elements",
    Roller,
    [roll: fn
       () -> 2
       () -> 5
    end]
  ) do
    assert VolunteerFinder.find(["John", "Andrew"])
    == [%{name: "Andrew", score: 5}, %{name: "John", score: 2}]
  end
end

【问题讨论】:

    标签: unit-testing testing erlang elixir mox


    【解决方案1】:

    我找到了一个可行的解决方案,但我不确定我是否满意:

      test_with_mock(
        "Find volunteer for list with two elements",
        Roller,
        [roll: fn () -> mock_roll end]
      ) do
        send self(), {:mock_return, 1}
        send self(), {:mock_return, 5}
    
        assert VolunteerFinder.find(["Sergey", "Borys"])
        == [%{name: "Borys", score: 5}, %{name: "Sergey", score: 1}]
      end
    
      def mock_roll do
        receive do
          {:mock_return, value} ->
            value
        after
          0 ->
            raise "No mocked returns received"
        end
      end
    

    有没有更优雅的解决问题的方法?

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      相关资源
      最近更新 更多