0

当我运行单元测试时,我将 lager 作为依赖应用程序启动,但由于某种原因,被测代码看不到它。

-module(main_tests).
-include_lib("eunit/include/eunit.hrl").

main_test_() ->
{foreach,
 fun distr_setup/0,
 fun distr_cleanup/1,
 [
  fun must_retain/1
  ]}.

must_retain(_) ->
{"Should do ping pong when is fully initialized",
 fun() ->
     ?assertEqual(pong, abuse_counter:ping())
 end}.



%%------------------------------------------------------------------
distr_setup() ->
abuse_counter:start_link(),
ok.

distr_cleanup(_) ->
abuse_counter:stop(),
ok.

这是日志的输出,它抱怨 未定义啤酒{undef,[{lager,info,["up and running"],[]}尽管在运行输出中肯定存在。

这是我的运行方式:

erl -pa ebin/ ../../deps/*/ebin -s lager -eval 'eunit:test(main_tests,[verbose]),    init:stop().'

输出失败

Eshell V5.10.2  (abort with ^G)
1> 17:13:31.506 [info] Application lager started on node nonode@nohost
======================== EUnit ========================
module 'main_tests'
undefined
17:13:31.528 [error] CRASH REPORT Process <0.57.0> with 1 neighbours exited with reason: call to undefined function lager:info("up and running") in gen_server:init_it/6 line 328
*unexpected termination of test process*
::**{undef,[{lager,info,["up and running"],[]}**,
      {abuse_counter,init,1,[{file,"src/abuse_counter.erl"},{line,37}]},
      {gen_server,init_it,6,[{file,"gen_server.erl"},{line,304}]},
      {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}

=======================================================
 Failed: 0.  Skipped: 0.  Passed: 0.
 One or more tests were cancelled.

已经在谷歌和堆栈溢出上花费了 3-4 小时,但似乎没有任何效果。

一种选择是将这个调用隐藏在 ?INFO(Mgs) 宏后面,但不喜欢这个想法。

任何帮助将不胜感激。

4

1 回答 1

4

所以看起来src/abuse_counter.erl文件没有使用该{parse_transform, lager_transform}选项编译。

函数lager:info("message!"),真的不存在, parse_transform 转换lager:info("message!")成这个函数:lager:dispatch_log(info, Metadata, "message!", [], Size)

尝试使用该{parse_transform, lager_transform}选项重新编译您的模块。

拉格展示了如何做到这一点:链接到自述文件

于 2013-10-31T21:50:56.450 回答