简体   繁体   English

Phoenix:如何在控制台中获得conn%Plug.Conn {}

[英]Phoenix: How to get conn %Plug.Conn{} in the console

After

iex -S mix phx.server

I want to do some quick tests in the iex terminal, but some functions require the struct %Plug.Conn{} as an argument, for example I wanted to get the result of expression: 我想在iex终端中做一些快速测试,但是有些函数需要struct%Plug.Conn {}作为参数,例如我想得到表达式的结果:

MyAppWeb.Router.Helpers.confirmation_url(%Plug.Conn{}, :edit, "12345")

But I've got error: 但我有错误:

Phoenix endpoint not found in %{}

Is there a simple way of getting conn struct in the console? 有一种简单的方法在控制台中获取conn结构吗?

Router helper functions accept either a conn or an endpoint module as the first argument. 路由器帮助程序函数接受conn或端点模块作为第一个参数。 You can pass the endpoint module of your app when you want to generate a URL without a conn: 如果要生成不带conn的URL,可以传递应用程序的端点模块:

MyAppWeb.Router.Helpers.confirmation_url(MyAppWeb.Endpoint, :edit, "12345")

Edit: If you want to create a dummy conn that works with Router helpers, it seems like it's enough to put a %{phoenix_endpoint: MyAppWeb.Endpoint} value in conn.private as of Phoenix 1.3: 编辑:如果你想创建一个与路由器助手一起使用的虚拟连接,从凤凰1.3开始,在conn.private放置%{phoenix_endpoint: MyAppWeb.Endpoint}值似乎就足够了:

conn = %Plug.Conn{private: %{phoenix_endpoint: MyAppWeb.Endpoint}}
MyAppWeb.Router.Helpers.confirmation_url(conn, :edit, "12345")

The ConnCase test helpers use Phoenix.ConnTest.build_conn() to bootstrap a connection struct for the controller tests. ConnCase测试助手使用Phoenix.ConnTest.build_conn()来引导控制器测试的连接结构。

You can find the function here and either use it directly or follow its implementation and tweak it as you like. 你可以在这里找到这个功能,或者直接使用它,或者按照它的实现进行调整,并根据需要进行调整。

Why spending time with testing on the console. 为什么要花时间在控制台上进行测试。 Just write a test and use the 'ConnCase' which gives you the conn struct in your tests for free. 只需编写一个测试并使用'ConnCase',它可以免费为您提供测试中的conn结构。 During development you can also use the "test watch" package which will rerun your tests on every file change. 在开发过程中,您还可以使用“测试监视”软件包,该软件包将在每次文件更改时重新运行测试。

As soon as you switch to tdd as more time you will save with problems like this 一旦你切换到tdd,你将有更多的时间来保存这样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phoenix:如何从控制台测试控制器动作? - Phoenix: How to test controller actions from the console? 如何在 Lua 控制台/终端中获取颜色 - How to get color in Lua console/terminal 如何在 Python 中获取 Linux 控制台窗口宽度 - How to get Linux console window width in Python 如何使用javascript console.log在ssh中获得相等的间距 - How to get equal spacing in ssh with javascript console.log 如何在控制台或文件中获取OS X应用程序的输出? - How to get the output of an OS X application on the console, or to a file? 如何获得 FreeBSD 无头控制台/终端的 256 色支持? - How to get 256 color support for FreeBSD headless console/terminal? 如何获取 Intellij 的 output 控制台的终端宽度 - How to get terminal width of Intellij's output console 如果打包为应用程序,如何在控制台上获取Apple Script的输出? - How to get the output of an Apple Script on the console if it's packaged as an app? 如何在Ruby中获取/设置控制台光标位置(Windows) - How to get/set the console cursor position in Ruby (Windows) 如何通过 SSH 从远程机器获取完整的控制台 output - How to get complete console output from remote machine via SSH
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM