Elixir "mix test"将@doc例子视为测试



最近我写了以下功能:

  @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任务,并且出现以下错误:

错误

看起来Mix测试正在考虑@DOC注释中的示例作为单位测试。

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

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

您可能已经复制了一些样板测试代码,默认情况下可以启用医生。喜欢:

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

卸下医生钻头,你应该没事的。当然,您还可以将评论格式化看起来不像医生: - )

最新更新