Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

elixir - Following a book "Programming Phoenix >= 1.4", got stuck at "Testing Logged-Out Users"

I'm following a book "Programming Phoenix >= 1.4", got stuck at "Testing Logged-Out Users" (Integration Tests, page 161). Test fail.

video_controller_test.exs

    defmodule RumblWeb.VideoControllerTest do
    use RumblWeb.ConnCase, async: true
  
    test "requires user authentication on all actions", %{conn: conn} do
      Enum.each([
        get(conn, Routes.video_path(conn, :new)),
        get(conn, Routes.video_path(conn, :index)),
        get(conn, Routes.video_path(conn, :show, "123")),
        get(conn, Routes.video_path(conn, :edit, "123")),
        put(conn, Routes.video_path(conn, :update, "123", %{})),
        post(conn, Routes.video_path(conn, :create, %{})),
        delete(conn, Routes.video_path(conn, :delete, "123")),
      ], fn conn ->
        assert html_response(conn, 302)
        assert conn.halted
      end)
    end
  end

mix test test/rumbl_web

 1) test requires user authentication on all actions (RumblWeb.VideoControllerTest)
     test/rumbl_web/controllers/video_controller_test.exs:4
     ** (FunctionClauseError) no function clause matching in Rumbl.Multimedia.list_user_videos/1

     The following arguments were given to Rumbl.Multimedia.list_user_videos/1:
     
         # 1
         nil
     
     Attempted function clauses (showing 1 out of 1):
     
         def list_user_videos(%Rumbl.Accounts.User{} = user)
     
     code: get(conn, Routes.video_path(conn, :index)),
     stacktrace:
       (rumbl) lib/rumbl/multimedia.ex:107: Rumbl.Multimedia.list_user_videos/1
       (rumbl) lib/rumbl_web/controllers/video_controller.ex:18: RumblWeb.VideoController.index/3
       (rumbl) lib/rumbl_web/controllers/video_controller.ex:1: RumblWeb.VideoController.action/2
       (rumbl) lib/rumbl_web/controllers/video_controller.ex:1: RumblWeb.VideoController.phoenix_controller_pipeline/2
       (phoenix) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
       (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.plug_builder_call/2
       (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:225: Phoenix.ConnTest.dispatch/5
       test/rumbl_web/controllers/video_controller_test.exs:7: (test)


Finished in 0.2 seconds
4 tests, 1 failure

My repo: https://github.com/zervis/rumbl

question from:https://stackoverflow.com/questions/65645162/following-a-book-programming-phoenix-1-4-got-stuck-at-testing-logged-out

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no current user logged in, hence Multimedia.list_user_videos/1 hets called with nil and there is no such clause handling nil (it expects the user struct only.)

I believe videos resource should go through :authenticate_user plug, as manage does.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...