Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir Assert conn在凤凰城被重定向_Elixir_Phoenix Framework - Fatal编程技术网

Elixir Assert conn在凤凰城被重定向

Elixir Assert conn在凤凰城被重定向,elixir,phoenix-framework,Elixir,Phoenix Framework,请im写一个测试,将检查用户是否重定向到一个特定的URL,如果他们登录并试图访问某些URL #auth.ex def authenticated_user(conn,_opts) do if conn.assigns.current_user do conn |> redirect(to: Helpers.user_path(conn,:dashboard)) |> halt() end conn end # router.ex pipe_thr

请im写一个测试,将检查用户是否重定向到一个特定的URL,如果他们登录并试图访问某些URL

#auth.ex
def authenticated_user(conn,_opts) do
  if conn.assigns.current_user do
    conn
    |> redirect(to: Helpers.user_path(conn,:dashboard))
    |> halt()
  end
  conn
end

# router.ex
pipe_through [:browser, :authenticated_user]
get "/signup", UserController, :new
get "/signin", SessionController, :new

#_test.ex
setup %{conn: conn} = config  do
  if email = config[:login_as ] do
    user = insert_user(%{email: "demo"})
    conn = assign(build_conn(),:current_user, user)
    {:ok, conn: conn, user: user}
  else
    :ok
  end
end


@tag login_as: "test user "
test "redirect already logged user when /signin or /signup is visited ", %{conn: conn} do
  Enum.each([
    get(conn, session_path(conn, :new)),
    get(conn, user_path(conn, new)),
  ], fn conn ->
    assert redirected_to(conn,302) == "/dashboard" 
  end)
end

实施后测试仍然失败。请问我错过了什么

尝试删除最后一个
连接。测试了您的代码,最终返回状态
200

#auth.ex
def authenticated_user(conn,_opts) do
if conn.assigns.current_user do
conn #<-- 302 returned here
|> redirect(to: Helpers.user_path(conn,:dashboard))
|> halt()
end
#conn <-- Remove this line, "" returned here
end
#auth.ex
def身份验证用户(conn,opts)do
如果conn.assigns.current_用户
连接#重定向(到:Helpers.user_路径(连接,:仪表板))
|>暂停
结束

#conn实际上,正确的实现需要处理else情况。插头总是不包括要重新调谐的接头

#auth.ex
def authenticated_user(conn,_opts) do
  # Don't use the `.` below. It will fail if the map does not have the key.
  if conn.assigns[:current_user] do    
    conn
    |> redirect(to: Helpers.user_path(conn,:dashboard))
    |> halt()   # stops the remaining plugs from being run.
  else
    # just returning conn will allow the requested page to be rendered
    conn
  end
end

请添加ex_unit outputRuntimeError预期重定向,状态为302,Get:200这表明问题出在控制器中。请发布
SessionController.create
UserController.create
@MikeBuhot的控制器代码,我已经用相关代码修改了我的问题,代码看起来也不错。测试如何建立连接当前用户?这显然是一个更好、更准确的解决方案