Is it possible to inspect the internal state of a gen_server after a callback function has been called? I would rather not change the API of my server here.
How to test gen_server internal state with eunit
683 views Asked by Rich Ashworth At
2
There are 2 answers
0
On
Maybe, you would found useful another approach to unit-testing gen_servers. Instead of running gen_server process and testing its behaviour, you can directly test gen_server callbacks and then inspect its state transitions.
For example:
-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.
See discussion of this approach here Also, if you dealing with realy complex states and state transitions, maybe you would be found proper helpful.
You could use
sys:get_state/1
which works nicely with all gen's.