vendredi 28 octobre 2016

Matching Plug Connection Adapter in Protocol's For

I would like to create a mocked HTTP request for mix test of App.Controller.sign_up/2. How to match %Plug.Conn{adapter: {Plug.Adapters.Test.Conn ... in protocol's for?

Caller

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

Protocol and Implementations

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

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

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

defmodule App.AppHelper do
  def verify(conn, params) do
    App.AppRequest.request conn, params
  end
end

Aucun commentaire:

Enregistrer un commentaire