如何在 Elixir 库 Ecto/Repo 中 mock/stub 方法?

How to mock/stub methods in Elixir library Ecto/Repo?

请告诉我如何 mock/stub Repo 模块的方法用于我的测试?

例如:

  link = Repo.get_by(Link, short_url: url)
  db_count = Repo.aggregate(Link, :count, :id)

我需要 Repo.aggregate returns 10000000000 进行测试。与 Repo.get_by.

相同

怎么做?

Elixir 中测试模块隔离的最佳方法是什么?

谢谢!

这是来自 https://github.com/gialib/ex_mock 自述文件的示例:

defmodule MyTest do
  use ExUnit.Case, async: false

  import ExMock

  test "test_name" do
    with_mock HTTPotion, [get: fn(_url) -> "<html></html>" end] do
      HTTPotion.get("http://example.com")
      # Tests that make the expected call
      assert called HTTPotion.get("http://example.com")
    end
  end
end