ruslander
ruslander

Reputation: 3875

Start dependent application with eunit

I start lager as a dependent application when I run a unit test but for some reason the code under test does not see it.

-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.

Here is the output of the log which is complaining that lager is not defined {undef,[{lager,info,["up and running"],[]} though in the run output is definitely there.

Here is how I run it:

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

Fails with the output

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.

Already spent 3-4h hours on google and stack overflow but nothing seems to work.

One option is to hide this call behind a ?INFO(Mgs) macro but do not like the idea.

Any help will be highly appreciated.

Upvotes: 0

Views: 542

Answers (1)

Daniel
Daniel

Reputation: 491

So it looks like the src/abuse_counter.erl file wasn't compiled with the {parse_transform, lager_transform} option.

The function lager:info("message!"), really doesn't exist, the parse_transform transforms the lager:info("message!") into this function: lager:dispatch_log(info, Metadata, "message!", [], Size).

Try recompiling your modules with the {parse_transform, lager_transform} option.

Lager shows how to do this: link to the Readme.

Upvotes: 4

Related Questions