【问题标题】:undefined _path after mix test混合测试后未定义的_path
【发布时间】:2018-10-11 17:20:52
【问题描述】:

我有路线/teams 和路径team_path 通过mix phx.routes。但是我收到undefined function team_path/3

下面是我的测试代码:

describe "create team" do
   test "renders team when data is valid", %{conn: conn} do
      conn = post(conn, team_path(conn, :create, team: @team_attrs))

      assert %{"id" => id} = json_response(conn, 201)["data"]
    end

    test "renders errors when data is invalid", %{conn: conn} do
      conn = post(conn, team_path(conn, :create, team: @invalid_attrs))
      assert json_response(conn, 400)["errors"] != %{}
    end
  end

其他信息:

  • Phoenix 1.4-beta
  • 已安装phoenix_swagger
  • 它是一个 API,所以一切都在范围内 /api

【问题讨论】:

标签: elixir phoenix-framework


【解决方案1】:

我认为 POST 调用的有效负载位于 post() 函数中,而您已将其放入 team_path 助手中。你可以试试:

conn = post(conn, team_path(conn, :create), %{team: @team_attrs})

参考文档链接:

https://hexdocs.pm/phoenix/Phoenix.ConnTest.html#post/3

【讨论】:

  • 仍然相同并收到相同的错误:Compilation error in file test/futsal_web/controllers/team_controller_test.exs == ** (CompileError) test/futsal_web/controllers/team_controller_test.exs:63: undefined function team_path/2 (stdlib) lists.erl:1338: :lists.foreach/2 (stdlib) erl_eval.erl:677: :erl_eval.do_apply/6 (elixir) lib/code.ex:767: Code.require_file/2 (elixir) lib/kernel/parallel_compiler.ex:209: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
  • 您能否运行mix phx.routes 并验证team_path 存在并且具有:create 操作?
  • 是的,它有:team_path POST /api/teams FutsalWeb.TeamController :create * /api FutsalWeb.PhoenixSwagger.Plug.SwaggerUI [otp_app: :futsal, swagger_file: "swagger.json"]
  • 您的测试套件是否导入了所有路由器助手?通常你只需要FutsalWeb.Router.Helpers 或类似的,其中包含路由辅助函数(通常在conn_case.ex 中)。我不确定 PhoenixSwagger 是如何工作的,但您可能需要在 conn_test.ex 中导入 Swagger 路由器的助手。
猜你喜欢
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2018-07-06
  • 2020-09-25
  • 1970-01-01
  • 2013-07-28
相关资源
最近更新 更多