【问题标题】:Elixir "mix test" considers @doc examples as testsElixir“混合测试”将@doc 示例视为测试
【发布时间】:2017-05-10 00:04:11
【问题描述】:

最近写了一个如下函数:

  @doc """
  Creates a new deck, shuffles its cards and retrieves a hand of
  cards from it.
  The `hand_size` argument indicates the size of the hand.

  ## Examples
      iex> {hand, _deck_remainder} = Cards.create_hand 2
      iex> hand
      ["Ace of Clubs", "Four of Hearts"]
  """
  def create_hand(hand_size) do
    create_deck
    |> shuffle
    |> deal(hand_size)
  end

需要考虑的要点:

  • create_deck/0 返回一个字符串列表,如 ["Two of Clubs", "Four of Hearts"]
  • shuffle/1 获取字符串列表并使用 Enum.shuffle/1 将它们打乱
  • deal/2 返回一个像 {["Ace of Spades"], ["Five of Clubs"]} 这样的元组

然后我运行mix test任务,出现如下错误:

ERROR

看起来 mix test 正在考虑将 @doc 注释中的示例作为单元测试。

由于shuffle/1 随机排列列表中的字符串,示例(作为单元测试)崩溃。

我想从mix test 中排除这些示例...这样做是个好主意吗?如果是这样,我该如何排除它们?

【问题讨论】:

  • 请参阅hexdocs.pm/ex_unit/ExUnit.DocTest.html,您需要为您的模块禁用doctest。将来,请以文本而不是图像的形式包含错误。
  • 感谢@sobolevn 提供的信息和建议

标签: elixir elixir-mix


【解决方案1】:

您可能已经复制了一些默认启用 doctests 的样板测试代码。喜欢:

defmodule MyModule.Test do
  use ExUnit.Case, async: true
  doctest MyModule
end

删除 doctest 位,你应该没问题。当然,您也可以将评论格式化为看起来不像 doctest :-)

【讨论】:

  • 谢谢你=D!我没有注意到模块中的那一行。
猜你喜欢
  • 1970-01-01
  • 2020-10-24
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多