我想在我的elixir应用程序的测试中使用我的一个依赖项的测试目录中的模块.我想知道是否有办法做到这一点.谢谢.
我试过import <module name>,这给了我一个编译错误elixir module is not loaded and could not be found.
Elixir进程有自己的堆. 如果进程想要与另一个进程共享数据结构,那怎么可能呢?我想到的一个答案是,该进程向包含数据结构的其他进程发送消息.这是否意味着整个数据结构从一个堆复制到另一个堆?如果这是真的,那不是效率低下吗?
如何使用该Enum函数在Elixir中将两个数组合并在一起?
例如
points = [[1.0, 2.0],
[5.0, 3.0],
[2.0, 4.0],
[3.0, 1.0],
[2.0, 2.0],
[4.0, 3.0],
[1.0, 2.0],
[2.0, 5.0],
[14.0, 13.0],
[11.0, 12.0],
[12.0, 15.0],
[32.0, 25.0]]
results = [1.0,
3.0,
2.0,
1.0,
3.0,
2.0,
1.0,
3.0,
2.0,
1.0,
3.0,
2.0]
Run Code Online (Sandbox Code Playgroud)
组合数组(添加列)应如下所示:
combined = [[1.0, 2.0, 1.0],
[5.0, 3.0, 3.0],
[2.0, 4.0, 2.0],
[3.0, 1.0, 1.0],
[2.0, 2.0, 3.0],
[4.0, 3.0, 2.0],
[1.0, 2.0, 1.0],
[2.0, 5.0, 3.0],
[14.0, 13.0, 2.0],
[11.0, 12.0, 1.0],
[12.0, 15.0, 3.0], …Run Code Online (Sandbox Code Playgroud) 我知道两者之间的区别; assoc_constraint使用ecto模式验证外键约束,foreign_key_constraint使用db.
你为什么要用assoc_constraint这种情况?
我正在使用edeliver在aws上部署.部署运行正常,但是当我尝试在控制台中访问该站点时curl localhost:8888,我收到connection refused错误.
如果我尝试启动应用程序./rel/bin/app_name console,我会得到一个(RuntimeError) expected the PORT environment variable to be set.但是我config/prod.exs看起来像这样.
use Mix.Config
config :elixir_deploy, ElixirDeployWeb.Endpoint,
load_from_system_env: true,
http: [port: 8888],
ssl: false,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"
config :logger, level: :info
import_config "prod.secret.exs"
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?如果我PORT=8888在手动启动之前设置了它,它可以工作,但我宁愿自动启动edeliver
按照本教程 .. 我正在玩Elixir并且Phoenix Framework是第一次。
我有一个简单的客户端/服务器应用程序。
chat / lib / chat_web / room_channel.ex:
defmodule ChatWeb.RoomChannel do
use Phoenix.Channel
def join("room:lobby", _message, socket) do
{:ok, socket}
end
def join("room:" <> _private_room_id, _params, _socket) do
{:error, %{reason: "unauthorized"}}
end
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
end
Run Code Online (Sandbox Code Playgroud)
我想阻止空的传入消息(body为空字符串)
def handle_in("new_msg", %{"body" => body}, socket) do
# I guess the code should be here..
broadcast! socket, "new_msg", %{body: body} …Run Code Online (Sandbox Code Playgroud) 我知道您可以使用IEx调试webapp但是可以在测试中设置断点吗?
例如,在下面的测试中,我想检查conn中的内容或检查任何其他变量或宏.
defmodule HelloWeb.PageControllerTest do
use HelloWeb.ConnCase
require IEx
test "GET /", %{conn: conn} do
IEx.pry
conn = get conn, "/"
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
end
Run Code Online (Sandbox Code Playgroud)
为了使它能够与webapp一起使用,你必须运行phoenix.server iex -S mix phoenix.server
但在这种情况下测试不是webapp所以它抱怨:
Cannot pry #PID<0.406.0> at ... Is an IEx shell running?
Run Code Online (Sandbox Code Playgroud) 我正在尝试在构建联盟结构时添加user_id,但它给了我这个错误:
** (KeyError) key :user_id not found in: %{__meta__: #Ecto.Schema.Metadata<:built, "leagues">, __struct__: Statcasters.Schema.League, id: nil, inserted_at: nil, name: nil, teams: #Ecto.Association.NotLoaded<association :teams is not loaded>, updated_at: nil, users: #Ecto.Association.NotLoaded<association :users is not loaded>, users_id: nil}
(stdlib) :maps.update(:user_id, {{:., [line: 14], [{:user, [line: 14], nil}, :id]}, [line: 14], []}, %{__meta__: #Ecto.Schema.Metadata<:built, "leagues">, __struct__: Statcasters.Schema.League, id: nil, inserted_at: nil, name: nil, teams: #Ecto.Association.NotLoaded<association :teams is not loaded>, updated_at: nil, users: #Ecto.Association.NotLoaded<association :users is not loaded>, users_id: nil})
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
def new(conn, _params) …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习长生不老药。在我使用过的大多数其他语言中,这将是一件容易的事。
但是,我似乎无法弄清楚如何在Elixir中按索引访问列表项,这是我在列表中查找中间项所需要的。任何澄清将不胜感激!
假设我正在测试属于Utils模块的函数
defmodule Test do
alias Module.Utils
test "test 1" do
Utils.some_function?(...)
end
test "test 2" do
Utils.some_function?(...)
end
end
Run Code Online (Sandbox Code Playgroud)
我可以将该函数重构或简化为:
import Utils.some_function as test_func()
Run Code Online (Sandbox Code Playgroud)
所以我不必编写模块名称并简化函数名称