协议中的匹配插头连接适配器

时间:2016-10-29 04:57:13

标签: testing elixir phoenix-framework mix

我想为mix test App.Controller.sign_up/2创建一个模拟的HTTP请求。如何匹配协议%Plug.Conn{adapter: {Plug.Adapters.Test.Conn ...中的for:

呼叫者

defmodule App.Controller do
  use App.Web, :controller
  def sign_up(conn, params) do
    case App.AppHelper.verify(conn, params) do
      {:ok, _} ->
    ...
    end
...

辅助

defmodule App.AppHelper do
  def verify(%Plug.Conn{adapter: {adapter, _}} = conn, params) do
    App.AppRequest.request adapter, params
  end
end

协议和实施

defprotocol App.AppRequest do
  @fallback_to_any true
  def request(adapter, params)
end

defimpl App.AppRequest, for: Plug.Adapters.Cowboy.Conn do
  def request(_, params) do
    #returns http request result
  end
end

defimpl App.AppRequest, for: Plug.Adapters.Test.Conn do
  def request(_, params) do
    #returns mocked result
  end
end

聚苯乙烯。在@Dogbert的帮助下编辑到我现在的状态

导致

** (Protocol.UndefinedError) protocol App.AppRequest not implemented for Plug.Adapters.Test.Conn

0 个答案:

没有答案