【发布时间】:2018-01-12 12:18:32
【问题描述】:
我一直在 Elixir 中尝试 doctests,它一直运行良好,直到我尝试进行字符串插值。
代码如下:
@doc"""
Decodes the user resource from the sub claim in the received token for authentication.
## Examples
iex> attrs = %{email: "test@example.com", password: "password", password_confirmation: "password"}
iex> {:ok, user} = Accounts.create_user(attrs)
iex> resource_from_claims(%{"sub" => "User:#{user.id}"})
{:ok, %User{}}
"""
def resource_from_claims(%{"sub" => "User:" <> id}) do
resource = Accounts.get_user(id)
case resource do
nil -> {:error, :no_result}
_ -> {:ok, resource}
end
end
运行mix test时出现此错误:
变量“user”不存在,正在扩展为“user()”,请使用括号消除歧义或更改变量名
我可以确认user 变量确实存在并且几乎可以在其他所有内容上工作,除非我尝试将它放在字符串插值中。
还有其他方法可以在 doctests 中进行字符串插值吗?
编辑:看起来我收到了这个错误,因为@doc 中的字符串插值部分实际上是在 doctest 范围之外运行,而是作为模块本身的一部分运行.我将看看是否有另一种方法可以在 doctest 的上下文中进行字符串插值。
【问题讨论】: