为了测试,我想制作一个模拟,如 José Valim在这里描述的
但我不断收到“CaseClauseError”错误。如果有兴趣,我在这里做了一个简单的项目。
当使用 Application.get_env 将模块绑定到模块属性时,例如
defmodule Att do
@file Application.get_env(:att, :file_module)
def hello do
file = Application.get_env(:att, :file_module)
file.hello()
# @file.hello
end
end
它导致以下编译错误:
mix compile
== Compilation error in file lib/att.ex ==
** (CaseClauseError) no case clause matching: [{:file, Att.Real, false, 2}]
lib/att.ex:5: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
如果我注释掉@file Application.get_env(:att, :file_module)
就没有问题,并且在 hello 函数中使用 file = Application.get_env(:att, :file_module) 就可以了。
问题不Application.get_env
具体,好像执行以下操作
defmodule Att do
# @file Application.get_env(:att, :file_module)
@file_mock Att.Mock
def hello do
file = Application.get_env(:att, :file_module)
file.hello()
Att.Mock.hello()
# @file.hello
end
end
然后@file_mock Att.Mock
导致相同的 CaseClauseError。问题似乎是将模块分配给模块属性。
@file
是一个特殊的模块属性,用于存储要在堆栈跟踪中使用的文件名。如果您改为将其分配给另一个名称,它应该可以正常工作。
@文件
更改属性后面的函数或宏的堆栈跟踪中使用的文件名,例如:
defmodule MyModule do @doc "Hello world" @file "hello.ex" def hello do "world" end end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句