我有以下模块
-module(bhavcopy_downloader).
-export([download/2]).
download(From, SaveTo) ->
{ok, {{Status, _}, _, Body}} = lhttpc:request(From, "GET", [], infinity),
case Status of
200 -> file:write(SaveTo, Body),
true;
_ -> false
end.并对上述代码进行以下测试
file_download_test_() ->
{foreach,
fun() ->
meck:new(lhttpc)
meck:new(file, [unstick])
end,
fun(_) ->
meck:unload(file),
meck:unload(lhttpc)
end,
{"saves the file at specified location",
fun() ->
meck:expect(lhttpc, request, 4, {ok, {{200, "OK"}, [], <<"response">>}}),
meck:expect(file, write_file, fun(Path, Data) ->
?assertEqual(Path, "~/Downloads/data-downloader/test.html"),
?assertEqual(Data, <<"response">>)
end),
?assertEqual(true, bhavcopy_downloader:download("http://google.com", "~/Downloads/data-downloader/test.html")),
?assert(meck:validate(file))
end}]
}.当我运行测试时,我得到了以下错误(为了简单起见,下面只粘贴了部分错误)。看着下面的错误,我感觉文件模块没有被模拟(或者当我使用meck:new(lhttpc)设置另一个模拟时,文件模块的模拟被覆盖。这里会出什么问题呢?
=ERROR REPORT==== 16-Feb-2013::20:17:24 ===
** Generic server file_meck terminating
** Last message in was {'EXIT',<0.110.0>,
{compile_forms,
{error,
[{[],
[{none,compile,
{crash,beam_asm,
{undef,
[{file,get_cwd,[],[]},
{filename,absname,1,
[{file,"filename.erl"},{line,67}]},
{compile,beam_asm,1,
[{file,"compile.erl"},{line,1245}]},
{compile,'-internal_comp/4-anonymous-1-',2,
[{file,"compile.erl"},{line,273}]},
{compile,fold_comp,3,
[{file,"compile.erl"},{line,291}]},
{compile,internal_comp,4,
[{file,"compile.erl"},{line,275}]},
{compile,'-do_compile/2-anonymous-0-',2,
[{file,"compile.erl"},{line,152}]}]}}}]}],
[{"src/lhttpc_types.hrl",
[{31,erl_lint,{new_builtin_type,{boolean,0}}},
{31,erl_lint,{renamed_type,bool,boolean}}]}]}}}发布于 2013-02-27 02:32:56
这是Meck的第22个陷阱,原因是Meck使用Erlang编译器,而Erlang编译器又使用file模块。当Meck尝试重新编译file模块时,它需要file模块(通过编译器),因此崩溃。
到目前为止,Meck还没有处理Meck文件模块。您最好的替代方法是将file模块调用包装在另一个模块中,并模拟此模块。
(理论上可以在Meck中通过使用编译器和代码服务器的内部来修复这个问题,例如,但是这相当棘手,需要很好地设计和测试)
https://stackoverflow.com/questions/14914730
复制相似问题