<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I would much prefer the latter - i.e., to test the public interface.  Otherwise you will end up with brittle tests that become a maintenance headache.  That said, I have had occasions where some functionality was much easier to test through the internal implementation rather than through the public API, so I think it depends to a degree on the context.<div><br></div><div>Cheers,</div><div><br></div><div>Steve<br><div><br><div>
<div><div><div>--------------------------</div><div><font size="2"><b>Steve Strong</b></font></div><div><font size="2"><b>Director, id3as</b></font></div><div><font size="2">+34 636451137</font></div><div><font size="2">@srstrong</font></div></div><div><font size="2"><br></font></div></div><br class="Apple-interchange-newline">

</div>
<br><div><div>On Aug 14, 2012, at 9:57 AM, Jan Vincent Liwanag <<a href="mailto:jvliwanag@gmail.com">jvliwanag@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Given the following code:</div><div><br></div><div>adder.erl:</div><div><br></div><div><font class="Apple-style-span" face="Courier">start(B) -> gen_server:start(?MODULE, B, []).</font></div><div><font class="Apple-style-span" face="Courier">add(Pid, N) -> gen_server:cast(Pid, {add, N}).</font></div><div><font class="Apple-style-span" face="Courier">get_total(Pid, N) -> gen_server:call(Pid, get_total).</font></div><div><font class="Apple-style-span" face="Courier"><br></font></div><div><font class="Apple-style-span" face="Courier">init(Total) -> {ok, Total}.</font></div><div><font class="Apple-style-span" face="Courier"><br></font></div><div><font class="Apple-style-span" face="Courier">handle_call(get_total, _From, Total) -> {reply, Total, Total}.</font></div><div><font class="Apple-style-span" face="Courier">handle_cast({add, N}, Total) -> {noreply, N+Total).</font></div><div><br></div><div><br></div><div>It would be better to have a test such that:</div><div><div><br></div><div><font class="Apple-style-span" face="Courier">{ok, P} = adder:start(0),</font></div><div><font class="Apple-style-span" face="Courier">adder:add(P, 5),</font></div><div><font class="Apple-style-span" face="Courier">adder:add(P, 3),</font></div><div><font class="Apple-style-span" face="Courier">adder:add(P, 2),</font></div></div><div><font class="Apple-style-span" face="Courier">?assertEqual(10, adder:get_running_total()).</font></div><div><br></div><div>Rather than:</div><div><br></div><div><font class="Apple-style-span" face="Courier">?assertEqual({noreply, 12}, handle_cast({add, 5}, 7)).</font></div><div><font class="Apple-style-span" face="Courier">?assertEqual({reply, 100, 100}, handle_call(get_total, somepid, 100)).</font></div><div><br></div><div>For one thing, if I change the gen_server state from an int to a record and fix up the code - the first set of tests should still succeed while the second won't.</div><br><div><div>On Aug 14, 2012, at 2:41 PM, Daniel Eliasson wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">It should be easy enough to unit test such things, just run a handle_call with the right arguments, and verify that the output and new state is correct?<div><br></div><div>I don't think there's a need to actually start the gen_server itself and unit test the callbacks by going through gen_server:call, if that's what you mean. In what way do you find it to be fragile?<div>
<br></div><div>Best,</div><div>Daniel<br><br><div class="gmail_quote">On 14 August 2012 03:48, Jan Vincent Liwanag <span dir="ltr"><<a href="mailto:jvliwanag@gmail.com" target="_blank">jvliwanag@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi,</div><div><br></div><div>What are your thoughts on unit testing gen_server (or other gen_*) callbacks (handle_call, etc) directly? Is this an ok practice? Or should unit tests test only the public api?</div>
<div><br></div><div>On my end, I find testing gen_server callbacks to be rather fragile. Eager to hear out other thoughts.</div><div><br></div><div>Thanks,</div><br><div>
<span style="text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><span style="text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<div>Jan Vincent Liwanag</div><div><a href="mailto:jvliwanag@gmail.com" target="_blank">jvliwanag@gmail.com</a></div><div><a href="tel:%2B63%20%28999%29%20888-0247" value="+639998880247" target="_blank">+63 (999) 888-0247</a></div>
</div></span></span>
</div>
<br></div><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>
</blockquote></div><br><div apple-content-edited="true">
<span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Jan Vincent Liwanag</div><div><a href="mailto:jvliwanag@gmail.com">jvliwanag@gmail.com</a></div><div>+63 (999) 888-0247</div></div></span></span>
</div>
<br></div>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote></div><br></div></div></body></html>