콜백 함수가 호출 된 후 gen_server의 내부 상태를 검사 할 수 있습니까? 차라리 내 서버의 API를 변경하지 않을 것입니다.eunit을 사용하여 gen_server 내부 상태를 테스트하는 방법
2
A
답변
6
모든 세대에서 잘 작동하는 sys:get_state/1
을 사용할 수 있습니다.
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가 도움이 발견 될 것이다.