Faking a response to a gen_server call

I’ve been trying to write a unit test for an FSM, that makes a call to a gen_server:

Result = gen_server:call(Pid, foo).

I wanted to fake the response to the call, without needing to spin up a gen_server instance. My first attempt didn’t really get me anywhere:

Result = ok,
Pid = spawn(fun() ->
    receive
        _ -> Result
    end
end).

But some digging through the OTP source revealed the solution:

Result = ok,
Pid = spawn(fun() ->
    receive
        {_, {Caller, Ref}, _} -> Caller ! {Ref, Result}
    end
end).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s