jeudi 11 janvier 2018

How can I do string interpolation inside a doctest in Elixir?

I've been trying out doctests in Elixir and it has been working out pretty well until I tried doing string interpolation.

Here is the code:

  @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

I get this error when running mix test:

variable "user" does not exist and is being expanded to "user()", please use parentheses to remove the ambiguity or change the variable name

I can confirm that the user variable does exist and works on just about everything else unless I try to put it inside a string interpolation.

Is there another way to do string interpolation inside doctests?

Aucun commentaire:

Enregistrer un commentaire