<div dir="ltr">Hi,<div><br></div><div style>I think I do prefer the approach #2 (the &quot;mock helper&quot; class)</div><div style><br></div><div style>-M</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Fri, Jun 7, 2013 at 10:32 AM, Christos Vasilakis <span dir="ltr">&lt;<a href="mailto:cvasilak@gmail.com" target="_blank">cvasilak@gmail.com</a>&gt;</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">Hi team,<div><br></div><div>for further improvements of our unit tests we have switched the http mocking mechanism we use (our own NSURLProtocol impl)  to the popular OHHTTPStubs[1] project,  a library currently recommended by the AFNetworking networking lib we use.</div>
<div><br></div><div>The basic mechanism is straightforward to use and encapsulated in one method:</div><div><br></div><div><span style="line-height:16px;font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace">return [</span><span style="line-height:16px;color:rgb(51,51,51);font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace">OHHTTPStubsResponse</span><span style="line-height:16px;font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace"> </span><span style="line-height:16px;color:rgb(51,51,51);font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace">responseWithData</span><span style="line-height:16px;font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-weight:bold">:</span><span style="line-height:16px;color:rgb(51,51,51);font-size:12px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace">data</span></div>
<div><pre style="line-height:16px;width:744px;font-size:12px;margin-bottom:0px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin-top:0px;word-wrap:break-word;padding:0px"><div>                                  <span>statusCode:</span><span style="color:rgb(51,51,51)">status</span></div>
<div>                                <span>responseTime:</span><span style="color:rgb(51,51,51)">responseTime</span></div><div>                                     <span>headers:</span><span style="color:rgb(51,51,51)">headers</span><span>];</span></div>
</pre><div><br></div></div><div>in which a stubbed response is returned to the client.</div><div><br></div><div>Now, based on this mechanism,  we have abstracted a bit and created methods such as:</div><div><br></div><pre style="line-height:16px;font-size:12px;width:744px;margin-bottom:0px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin-top:0px;word-wrap:break-word;padding:0px">
<div><span style="font-weight:bold"> +</span> <span>(</span><span style="color:rgb(68,85,136);font-weight:bold">void</span><span>)</span><span style="color:rgb(51,51,51)">mockResponse</span><span style="font-weight:bold">:</span><span>(</span><span style="color:rgb(51,51,51)">NSData</span><span style="font-weight:bold">*</span><span>)</span><span style="color:rgb(51,51,51)">data</span><span>;</span></div>
<div><span> <span style="font-weight:bold">+</span> <span>(</span><span style="color:rgb(68,85,136);font-weight:bold">void</span><span>)</span><span style="color:rgb(51,51,51)">mockResponseStatus</span><span style="font-weight:bold">:</span><span>(</span><span style="color:rgb(68,85,136);font-weight:bold">int</span><span>)</span><span style="color:rgb(51,51,51)">status</span><span>;</span></span></div>
<div> <span style="font-weight:bold">+</span> <span>(</span><span style="color:rgb(68,85,136);font-weight:bold">void</span><span>)</span><span style="color:rgb(51,51,51)">mockResponseTimeout</span><span style="font-weight:bold">:</span><span>(</span><span style="color:rgb(51,51,51)">NSData</span><span style="font-weight:bold">*</span><span>)</span><span style="color:rgb(51,51,51)">data</span> <span style="color:rgb(51,51,51)">status</span><span style="font-weight:bold">:</span><span>(</span><span style="color:rgb(68,85,136);font-weight:bold">int</span><span>)</span><span style="color:rgb(51,51,51)">status</span> <span style="color:rgb(51,51,51)">responseTime</span><span style="font-weight:bold">:</span><span>(</span><span style="color:rgb(51,51,51)">NSTimeInterval</span><span>)</span><span style="color:rgb(51,51,51)">responseTime</span><span>;</span></div>
<div><br></div></pre><div>This gives the advantages that a) clearly indicate what http scenario we are testing  and b) remove params that don&#39;t make sense for the particular scenario under testing e.g. that is we simulate a status of  (404) but we need to pass all params eg. data, interval, timeout, etc.  But this doesn&#39;t limit us, we can do that if we want and use the full blown method with all the params attached.</div>
<div><br></div><div>I have created two branches in my fork, one that uses a blocks approach inside the testing class [2] and one that the functionality is extracted in a helper class that the testing classes can use [3].  The second approach was created because there was common code and didn&#39;t want to duplicate it over the testing classes.  </div>
<div><br></div><div>I would be interesting to know what is your comments on it?</div><div><br></div><div>Thanks,</div><div>Christos</div><div><br></div><div><br></div><div>[1] <a href="https://github.com/AliSoftware/OHHTTPStubs" target="_blank">https://github.com/AliSoftware/OHHTTPStubs</a></div>
<div>[2] <a href="https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs/AeroGear-iOS/AeroGear-iOSTests/AGRestAdapterTests.m#L33-L51" target="_blank">https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs/AeroGear-iOS/AeroGear-iOSTests/AGRestAdapterTests.m#L33-L51</a></div>
<div>[3] <a href="https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs.helper/AeroGear-iOS/AeroGear-iOSTests/utils/AGHTTPMockHelper.m" target="_blank">https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs.helper/AeroGear-iOS/AeroGear-iOSTests/utils/AGHTTPMockHelper.m</a></div>
<div>[4] </div><div> </div><div><br></div></div><br>_______________________________________________<br>
aerogear-dev mailing list<br>
<a href="mailto:aerogear-dev@lists.jboss.org">aerogear-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/aerogear-dev</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Matthias Wessendorf <br>
<br>blog: <a href="http://matthiaswessendorf.wordpress.com/" target="_blank">http://matthiaswessendorf.wordpress.com/</a><br>sessions: <a href="http://www.slideshare.net/mwessendorf" target="_blank">http://www.slideshare.net/mwessendorf</a><br>
twitter: <a href="http://twitter.com/mwessendorf" target="_blank">http://twitter.com/mwessendorf</a>
</div>