相关疑难解决方法(0)

当我在会话中测试需要user_id的phoenix动作时,如何在设置中设置会话?

我有一个测试需要在测试之前将user_id设置为session,因为此操作需要知道current_user.

setup do
  %User{
    id: 123456,
    username: "lcp",
    email: "abc@gmail.com",
    password: Comeonin.Bcrypt.hashpwsalt("password")
  } |> Repo.insert

  {:ok, user: Repo.get(User, 123456) }
end

test "POST /posts", context do
  # conn = conn()
  #      |> put_session(:user_id, context[:user].id)
  #      |> post("/posts", %{ post: %{ title: "title", body: "body" } })

  # assert get_flash(conn, :info) == "Post created successfully."

  # updated to => 
  conn = conn()
        |> Map.put(:secret_key_base, String.duplicate("abcdefgh", 8))
        |> Plug.Session.call(@session)
        |> Plug.Conn.fetch_session
        |> put_session(:user_id, context[:user].id)
        |> post("/posts", %{ post: %{ title: "title", body: …
Run Code Online (Sandbox Code Playgroud)

testing session elixir phoenix-framework

15
推荐指数
1
解决办法
6904
查看次数

标签 统计

elixir ×1

phoenix-framework ×1

session ×1

testing ×1