2016-12-02 76 views
0

我正在編寫一個項目,我想測試,自動與ExUnit和Interactivly與IEX。說我的項目是這樣的:使燈具和測試幫助功能可用於ExUnit和iex

[[email protected] sample]$ tree 
. 
├── config 
│   └── config.exs 
├── fixtures 
│   └── complex_struct.exs 
├── lib 
│   └── the_function.ex 
├── mix.exs 
├── README.md 
└── test 
    └── the_test.exs 

4 directories, 7 files 
[[email protected] sample]$ cat lib/the_function.ex 
defmodule TheFunction do 
    def the_function ({a, b, c}) do 
     a/b + c 
    end 
end 
[[email protected] sample]$ cat fixtures/complex_struct.exs 

defmodule ComplexStruct do 
    def complex_struct do 
     {2, 1, 1} 
    end 
end 
[[email protected] sample]$ cat test/the_test.exs 
defmodule IexandtestTest do 
    Code.load_file("fixtures/complex_struct.exs") 
    use ExUnit.Case 
    doctest Iexandtest 

    test "the test" do 
    assert (TheFunction.the_function (ComplexStruct.complex_struct())) == 3 
    end 
end 

我現在可以運行混合測試,它會發現燈具/ complex_struct.exs使測試成功執行。我也喜歡使用下面的命令來調試我的代碼

iex -S mix 

這樣我就可以訪問lib/the_function.ex並且可以調試它。

iex(1)> TheFunction.the_function({1,2,3}) 
3.5 

但我不得不燈具沒有訪問/ complex_struct.exs除非我加載它是這樣的:

iex(1)> TheFunction.the_function(ComplexStruct.complex_struct()) 
** (UndefinedFunctionError) undefined function ComplexStruct.complex_struct/0 (module ComplexStruct is not available) 
    ComplexStruct.complex_struct() 
iex(1)> Code.load_file("fixtures/complex_struct.exs") 
[{ComplexStruct, 
    <<70, 79, 82, 49, 0, 0, 5, 28, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 137, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, 100, 0, ...>>}] 
iex(2)> TheFunction.the_function(ComplexStruct.complex_struct()) 
3.0 

是什麼決定了什麼得到由IEX加載,如何才能讓所有的模塊在lib和所有當我運行iex -S混合時可用的燈具?

回答

1

只有您mix.exs函數返回值爲project/0:elixirc_paths指定的目錄中的文件被編譯到您的應用程序中。默認值:elixirc_paths["lib"]

來編譯fixtures藥劑的文件,你需要從exs擴展更改爲ex,然後添加fixtures:elixirc_paths

def project do 
    [app: :m, 
    version: "0.1.0", 
    ..., 
    elixirc_paths: ["lib", "fixtures"]] 
end 

此之後,你就可以從兩個iex和測試訪問ComplexStruct,並且您不再需要在測試模塊中調用Code.load_file