【问题标题】:Elixir/Phoenix/Guardian - Assigning current_user in conn doesn't work for testingElixir/Phoenix/Guardian - 在 conn 中分配 current_user 不适用于测试
【发布时间】:2018-04-24 12:43:17
【问题描述】:

如果有这样的控制器

def index(%{assigns: %{current_user: %User{} = current_user}}} = conn, params) do
    ## User exists
end

def index(conn, params) do
    ## No user exists
end

如果我运行这个测试,我希望它调用第一个 #index 操作,但它调用第二个操作,没有用户,并且 conn 分配的是 %{current_user: nil}

test "GET /posts", %{conn: conn} do
    user = insert(:user)
    conn = assign(conn, :current_user, user)
    get(conn, post_path(conn, :index))
end

为什么没有正确分配 current_user?为什么会消失?

【问题讨论】:

  • “如果我运行这个测试”——哪个测试?
  • @mudasobwa 作为控制器测试。
  • 您是否尝试过在测试中使用特别是“Plug.Conn.assign”

标签: elixir phoenix-framework guardian


【解决方案1】:

我不确定,但可以试试这个

def index (conn,params)
 current_user = conn.assigns.current_user
## User exists
end

试试这个

【讨论】:

  • 如果这可行,那么他的第一个函数将具有模式匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多