Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/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
Testing 使用Elixir/Phoenix测试api调用_Testing_Elixir_Phoenix Framework - Fatal编程技术网

Testing 使用Elixir/Phoenix测试api调用

Testing 使用Elixir/Phoenix测试api调用,testing,elixir,phoenix-framework,Testing,Elixir,Phoenix Framework,我是Elixir和Phoenix的新手,我正在尝试创建一个web服务来赞美我的网站。首先,我只想通过从json文件导入一些数据来测试我的新数据结构。我想我应该做个测试。我已经阅读了基本指南(包括测试部分),但还没有找到任何关于测试api调用的内容 从下面的代码中,当我运行混合测试时,我得到以下错误: ** (ArgumentError) flash not fetched, call fetch_flash/2 这在发出呼叫并返回连接的线路上失败。我假设我打错了电话/错过了什么?有没有我遗漏的

我是Elixir和Phoenix的新手,我正在尝试创建一个web服务来赞美我的网站。首先,我只想通过从json文件导入一些数据来测试我的新数据结构。我想我应该做个测试。我已经阅读了基本指南(包括测试部分),但还没有找到任何关于测试api调用的内容

从下面的代码中,当我运行
混合测试时,我得到以下错误:

** (ArgumentError) flash not fetched, call fetch_flash/2
这在发出呼叫并返回连接的线路上失败。我假设我打错了电话/错过了什么?有没有我遗漏的文档,或者有人能给我举个好例子吗

下面是我的
路由器.ex
中的一个片段:

  scope "/", ContactsApp do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    resources "/contacts", ContactsController
  end

  # Other scopes may use custom stacks.
  scope "/api", ContactsApp do
    pipe_through :api

    get "/import", ContactsController, :import
  end
目前,我所做的只是复制
ContactsController.Create
方法并调用它
ContactsController.Import
。我还复制了“创建资源并在数据有效时重定向”测试,并使用了
:import
,而不是
:create

以下是完整的堆栈跟踪:

** (ArgumentError) flash not fetched, call fetch_flash/2
 stacktrace:
   (phoenix) lib/phoenix/controller.ex:997: Phoenix.Controller.get_flash/1
   (phoenix) lib/phoenix/controller.ex:982: Phoenix.Controller.put_flash/3
   (contacts_app) web/controllers/contacts_controller.ex:74: ContactsApp.LogController.stuff/2
   (contacts_app) web/controllers/contacts_controller.ex:1: ContactsApp.LogController.action/2
   (contacts_app) web/controllers/contacts_controller.ex:1: ContactsApp.LogController.phoenix_controller_pipeline/2
   (contacts_app) lib/phoenix/router.ex:261: ContactsApp.Router.dispatch/2
   (contacts_app) web/router.ex:1: ContactsApp.Router.do_call/2
   (contacts_app) lib/contacts_app/endpoint.ex:1: ContactsApp.Endpoint.phoenix_pipeline/1
   (contacts_app) lib/phoenix/endpoint/render_errors.ex:34: ContactsApp.Endpoint.call/2
   (phoenix) lib/phoenix/test/conn_test.ex:194: Phoenix.ConnTest.dispatch/5
   test/controllers/contacts_controller_test.exs:69

多亏了@Dogbert和@sobolevn,我才知道自己做错了什么。当代码由
mix phoenix.gen.html
生成时,控制器可能具有以下内容:

def create(conn, %{"contact" => contact_params}) do
  changeset = Contact.changeset(%Contact{}, contact_params)

  case Repo.insert(changeset) do
    {:ok, _contact} ->
      conn
      |> put_flash(:info, "Contact created successfully.")
      |> redirect(to: contact_path(conn, :index))
    {:error, changeset} ->
      render(conn, "new.html", changeset: changeset)
  end
end
当代码由
mix phoenix.gen.json
生成时,控制器包含稍微不同的代码:

def create(conn, %{"fred" => fred_params}) do
  changeset = Fred.changeset(%Fred{}, fred_params)

  case Repo.insert(changeset) do
    {:ok, fred} ->
      conn
      |> put_status(:created)
      |> put_resp_header("location", fred_path(conn, :show, fred))
      |> render("show.json", fred: fred)
    {:error, changeset} ->
      conn
      |> put_status(:unprocessable_entity)
      |> render(ContactsApp.ChangesetView, "error.json", changeset: changeset)
  end
end

在我复制和粘贴的代码中,有一个调用@Dogbert建议的
put_flash
(这意味着要使用
:浏览器
管道)。使用使用
mix phoenix.gen.json生成的代码解决了问题。

您是否在代码或测试中调用
get\u flash
?显然
conn=get conn,contacts\u path(conn,:import),log:@valid\u attrs
您是否尝试过
mix phoenix.gen.json
命令?它将为您生成测试。所以你可以看看是怎么做的。酷。这帮了大忙。谢谢