Plug.Conn.Unfetched不实现Access行为

时间:2016-11-23 02:26:17

标签: struct runtime-error elixir phoenix-framework

从下面的代码中,当我拨打conn.params["geo"]时,我收到以下错误:

test/plugs/geoip_test.exs:4
 ** (UndefinedFunctionError) function Plug.Conn.Unfetched.fetch/2 is undefined  (Plug.Conn.Unfetched does not implement the Access behaviour)
 stacktrace:
   (plug) Plug.Conn.Unfetched.fetch(%{:__struct__ => Plug.Conn.Unfetched, :aspect => :params, "geo" => "Mountain View, US", "ip" => "8.8.8.8"}, "geo")

...

defmodule AgilePulse.Plugs.GeoIPTest do
  use AgilePulse.ConnCase

  test "returns Mountain View for 8.8.8.8" do
    conn = build_conn
    params = Map.put(conn.params, "ip", "8.8.8.8")
    conn = Map.put(conn, :params, params) |> AgilePulse.Plugs.GeoIP.call(%{})

    assert conn.params["geo"] == "Mountain View, US"
  end

end

defmodule AgilePulse.Plugs.GeoIP do
  import Plug.Conn

  def init(opts), do: opts

  def call(%Plug.Conn{params: %{"ip" => ip}} = conn, _opts) do
    geo = set_geo(ip)
    params = Map.put(conn.params, "geo", geo)
    Map.put(conn, :params, params)
  end

  def call(conn, _opts), do: conn

  ...

end

有人可以告诉我为什么会失败以及适当的解决方案是什么? TY!

1 个答案:

答案 0 :(得分:5)

简短回答:改变一下:

params = Map.put(conn.params, "ip", "8.8.8.8")

要:

params = %{"ip": "8.8.8.8"}

说明:Phoenix.ConnTest.build_conn/0返回Connparams设置为%Plug.Conn.Unfetched{}。通过使用Map.put,您不会重置__struct__的值,而只会添加新密钥:

%Plug.Conn{ ...,
 params: %{:__struct__ => Plug.Conn.Unfetched, :aspect => :params,
   "ip" => "8.8.8.8"}, ... }

当您稍后致电params["geo"]时,Elixir会看到params是一个结构,并尝试在结构模块上调用fetch/2函数,而该模块并非如此。存在。要将params重置为普通地图(以便Elixir在使用方括号语法时调用Map.get),您只需执行params = %{"ip": "8.8.8.8"}