gpt4 book ai didi

elixir - 测试配置/系统环境 getter

转载 作者:行者123 更新时间:2023-12-02 04:12:38 24 4
gpt4 key购买 nike

我有一个名为 Auth.app_id 的函数,它会尝试首先从应用程序的配置中读取 app_id,如果缺少,将查找系统环境。

测试此类功能的最佳实践是什么?当然,事先在测试中使用 Application.put_envSystem.put_env 是非常糟糕的做法,因为我们使用全局变量进行操作,异步测试是不可能的。

test "getting config variables" do
Application.put_env(:appname, :app_id, "123")
assert Auth.app_id === "123"
end

test "getting env variables" do
System.put_env("APPNAME_APP_ID", "111")
assert Auth.app_id === "111"
end

这是 getter 函数内部的样子:

def app_id do
Application.get_env(
:appname,
:app_id,
System.get_env("APPNAME_APP_ID")
)
end

问题是我有很多使用这些 getter 的函数,比如返回带有 app_id 作为参数的 url。

最佳答案

无法回避这些变量是全局的这一事实(不考虑模拟)。我的建议是要么不要同时运行测试(不在 use ExUnit 中使用 async: true),要么不测试 app_id/0函数(因为无论如何它都很微不足道)。

关于elixir - 测试配置/系统环境 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35793871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com