2016-12-02 42 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