2016-06-12 63 views
0

我寫的真棒印記:藥劑:doctesting一個印記

defmodule Sigiltest do 

    @doc """ 
    An awesome sigil! 

    ### Usage 

     iex> ~a{I love elixir} 
     "I love elixir, awesome!" 
    """ 
    def sigil_a(content, _flags) do 
    "#{content}, awesome!" 
    end 
end 

下面是測試模塊:

defmodule SigiltestTest do 
    use ExUnit.Case 
    doctest Sigiltest 
end 

當我運行mix test我得到以下的輸出:

Compiled lib/sigiltest.ex 
** (CompileError) (for doctest at) lib/sigiltest.ex:7: undefined function sigil_a/2 
    (stdlib) lists.erl:1337: :lists.foreach/2 
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6 

有沒有辦法在sigil上運行doctests? 如果是這樣,怎麼樣?

回答

4

這是因爲sigil_a不存在於doctests正在運行的上下文中。您可以導入Sigiltest明確:

@doc """ 
An awesome sigil! 

### Usage 

    iex> import Sigiltest 
    iex> ~a{I love elixir} 
    "I love elixir, awesome!" 
""" 

或添加import: truedoctest調用:

doctest Sigiltest, import: true