[英]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.