我有一个测试需要在测试之前将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)