<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">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 class="p" style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; ">return [</span><span class="n" style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; color: rgb(51, 51, 51); ">OHHTTPStubsResponse</span><span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; "> </span><span class="n" style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; color: rgb(51, 51, 51); ">responseWithData</span><span class="o" style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; font-weight: bold; ">:</span><span class="n" style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; color: rgb(51, 51, 51); ">data</span></div><div><pre class="line-pre" style="font-size: 12px; line-height: 16px; font-family: Consolas, 'Liberation Mono', Courier, monospace; word-wrap: break-word; width: 744px; margin-top: 0px; margin-bottom: 0px; padding: 0px; background-color: rgb(255, 255, 255); position: static; z-index: auto; "><div class="line" id="file-gistfile1-c-LC2"> <span class="nl">statusCode:</span><span class="n" style="color: rgb(51, 51, 51); ">status</span></div><div class="line" id="file-gistfile1-c-LC3"> <span class="nl">responseTime:</span><span class="n" style="color: rgb(51, 51, 51); ">responseTime</span></div><div class="line" id="file-gistfile1-c-LC4"> <span class="nl">headers:</span><span class="n" style="color: rgb(51, 51, 51); ">headers</span><span class="p">];</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 class="line-pre" style="word-wrap: break-word; width: 744px; margin-top: 0px; margin-bottom: 0px; padding: 0px; background-color: rgb(255, 255, 255); position: static; z-index: auto; line-height: 16px; font-size: 12px; font-family: Consolas, 'Liberation Mono', Courier, monospace; "><div class="line" id="file-gistfile1-c-LC6"><span class="o" style="font-weight: bold; "> +</span> <span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold; ">void</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">mockResponse</span><span class="o" style="font-weight: bold; ">:</span><span class="p">(</span><span class="n" style="color: rgb(51, 51, 51); ">NSData</span><span class="o" style="font-weight: bold; ">*</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">data</span><span class="p">;</span></div><div class="line" id="file-gistfile1-c-LC6"><span class="p"> <span class="o" style="font-weight: bold; ">+</span> <span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold; ">void</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">mockResponseStatus</span><span class="o" style="font-weight: bold; ">:</span><span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold; ">int</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">status</span><span class="p">;</span></span></div><div class="line" id="file-gistfile1-c-LC7"> <span class="o" style="font-weight: bold; ">+</span> <span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold; ">void</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">mockResponseTimeout</span><span class="o" style="font-weight: bold; ">:</span><span class="p">(</span><span class="n" style="color: rgb(51, 51, 51); ">NSData</span><span class="o" style="font-weight: bold; ">*</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">data</span> <span class="n" style="color: rgb(51, 51, 51); ">status</span><span class="o" style="font-weight: bold; ">:</span><span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold; ">int</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">status</span> <span class="n" style="color: rgb(51, 51, 51); ">responseTime</span><span class="o" style="font-weight: bold; ">:</span><span class="p">(</span><span class="n" style="color: rgb(51, 51, 51); ">NSTimeInterval</span><span class="p">)</span><span class="n" style="color: rgb(51, 51, 51); ">responseTime</span><span class="p">;</span></div><div class="line" id="file-gistfile1-c-LC9"><br></div></pre><div>This gives the advantages that a) clearly indicate what http scenario we are testing and b) remove params that don'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'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'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">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">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">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></body></html>