2014-11-23 59 views

回答

1

也许,你会发现有用的另一种方法来单元测试gen_servers。 您可以直接测试gen_server回调,然后检查其状态转换,而不是运行gen_server进程并测试其行为。

例如:这种方法的here

-module(foo_server). 

%% Some code skipped 

handle_call({do_stuf, Arg}, _From, State) -> 
    NewState = modify_state(
    {reply, {stuf_done, Arg}, NewState}. 

%% Some code skipped 

-ifdef(TEST) 

do_stuf_test_() -> 
    {setup, 
     fun() -> 
      {ok, InitState} = foo_server:init(SomeInitParams), 
      InitState 
     end, 
     fun(State) -> 
      ok = foo_server:terminate(shutdown, State) 
     end, 
     fun(State) -> 
      Result = foo_server:handle_call({do_stuf, hello}, undefined, State), 
      [ 
       ?_assertMatch({reply, {stuf_done, hello}, _}, Result) 
      ] 
     end 
    } 
}. 

-endif. 

见讨论,如果你处理复杂真的状态和状态转换,也许你会发现proper很有帮助。