<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; dpettigrew</title>
	<atom:link href="/author/dpettigrew/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Using Method-Swizzling to help with Test Driven Development</title>
		<link>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/</link>
		<comments>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 08:00:22 +0000</pubDate>
		<dc:creator><![CDATA[dpettigrew]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[ocunit]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[swizzling]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3741</guid>
		<description><![CDATA[Introduction
In this blog post I will demonstrate how to mock HTTP requests using an Objective-C runtime dynamic method replacement technique known as method swizzling. I will show how this can be used in tandem with some unit test technologies to help with development of iOS web-service client side code.
In this example, the actual work of the HTTP request is embodied in my AsyncURLConnection class. It uses NSURLConnection to perform an NSURLRequest in an asynchronous manner, using completion handler blocks to return  ...]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>In this blog post I will demonstrate how to mock HTTP requests using an Objective-C runtime dynamic method replacement technique known as <em>method swizzling</em>. I will show how this can be used in tandem with some unit test technologies to help with development of iOS web-service client side code.</p>
<p>In this example, the actual work of the HTTP request is embodied in my AsyncURLConnection class. It uses NSURLConnection to perform an NSURLRequest in an asynchronous manner, using completion handler blocks to return the response to the caller. I could have chosen to mock the native APIs but mocking at the AsyncURLConnection level provides the desired effect more easily.</p>
<h2>Method Swizzling Helper Class</h2>
<p>This is a generic helper class for method swizzling (swapping). This gets used in tests as seen below. The</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">swizzleClassMethod<span style="color: #002200;">:</span>selector<span style="color: #002200;">:</span>swizzleClass<span style="color: #002200;">:</span></pre></td></tr></table></div>

<p>method takes a class and a method selector along with a class with an alternative implementation of the method given by the selector. The class swaps the implementation of the existing class method with the corresponding method in the swizzleClass.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;Foundation/Foundation.h&amp;gt;</span>
<span style="color: #6e371a;">#import &amp;lt;objc/runtime.h&amp;gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Swizzler <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>target_class selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>swizzleClass;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deswizzle;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;Swizzler.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Swizzler
&nbsp;
Method originalMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
Method swizzleMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
Swaps the implementation of an existing class method with the corresponding method in the swizzleClass.
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>targetClass selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>swizzleClass
<span style="color: #002200;">&#123;</span>
	originalMethod <span style="color: #002200;">=</span> class_getClassMethod<span style="color: #002200;">&#40;</span>targetClass, selector<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// save the oringinal implementation so it can be restored</span>
	swizzleMethod <span style="color: #002200;">=</span> class_getClassMethod<span style="color: #002200;">&#40;</span>swizzleClass, selector<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// save the replacement method</span>
	method_exchangeImplementations<span style="color: #002200;">&#40;</span>originalMethod, swizzleMethod<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// perform the swap, replacing the original with the swizzle method implementation.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Restores the implementation of an existing class method with its original implementation.
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deswizzle
<span style="color: #002200;">&#123;</span>
	method_exchangeImplementations<span style="color: #002200;">&#40;</span>swizzleMethod, originalMethod<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// perform the swap, replacing the previously swizzled method with the original implementation.</span>
	swizzleMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	originalMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Class to be mocked</h2>
<p>This is the interface of a class that connects and loads a URL. I will be replacing the implementation so its details are left out of here for brevity.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;Foundation/Foundation.h&amp;gt;</span>
&nbsp;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>completeBlock_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>errorBlock_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">@interface</span> AsyncURLConnection <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>data;
    completeBlock_t completeBlock;
    errorBlock_t errorBlock;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Asynchronously performs an HTTP GET - invokes one of the blocks depending on the response to the request 
*/</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>aCompleteBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>anErrorBlock;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Initializes and asynchronously performs an HTTP GET - invokes one of the blocks depending on the response to the request
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>aCompleteBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>anErrorBlock;
&nbsp;
<span style="color: #11740a; font-style: italic;">// If both HTTP Basic authentication optional parameters are non-empty strings, request will encode the URL with them.</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>authUserName; <span style="color: #11740a; font-style: italic;">// HTTP Basic authentication optional parameter</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>authPassword; <span style="color: #11740a; font-style: italic;">// HTTP Basic authentication optional parameter</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>accept; <span style="color: #11740a; font-style: italic;">// Optional parameter to set the response type accepted (default is @&quot;application/json&quot;)</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Example of Usage in a Test</h2>
<p>In this test I am testing that I am correctly processing the JSON data returned from the web service. However, I want to isolate the actual web service from the test. I want a test to validate the creation of the NSDictionary response object from the JSON.<br />
As an aside, by removing the Swizzler code, this test could be run against the actual web service; but that is not the intent here.<br />
Here is what the JSON response looks like &#8211;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;coaches&quot;</span> <span style="color: #339933;">:</span>
	<span style="color: #009900;">&#91;</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;per_page&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;total&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">11</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;total_pages&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The method under test here is:-</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadCollectionFromEndpoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>endpoint successBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadSuccessBlock<span style="color: #002200;">&#41;</span>successBlock failBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadFailBlock<span style="color: #002200;">&#41;</span>failBlock</pre></td></tr></table></div>

<p>This method calls the AsyncURLConnection class method</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock;</pre></td></tr></table></div>

<p>I will replace the existing implementation of that method with one that does not actually make an HTTP request, but instead gets JSON from a local file.</p>
<p>The unit test uses <a href="https://github.com/hamcrest/OCHamcrest">OCHamcrest</a> matchers and XCode&#8217;s built in <a href="http://www.sente.ch/software/ocunit/">OCUnit</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>test_loadPopularCoaches_success
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Test set-up</span>
    Swizzler <span style="color: #002200;">*</span>swizzler <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Swizzler new<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Replace the actual AsyncURLConnection method with our own test one.</span>
    <span style="color: #002200;">&#91;</span>swizzler swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>AsyncURLConnection class<span style="color: #002200;">&#93;</span> selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>completeBlock<span style="color: #002200;">:</span>errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>RequestPopularCoachesPassMock class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    __block <span style="color: #a61390;">BOOL</span> hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Test</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>endpoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@?per_page=6&amp;amp;page=1&amp;amp;popular=true&quot;</span>, kCoachesEndpoint<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>JSONLoader sharedJSONLoader<span style="color: #002200;">&#93;</span> loadCollectionFromEndpoint<span style="color: #002200;">:</span>endpoint successBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>collection<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>coaches <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;coaches&quot;</span><span style="color: #002200;">&#93;</span>;
        assertThat<span style="color: #002200;">&#40;</span>coaches, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
        <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
        <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>coach <span style="color: #a61390;">in</span> coaches<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>coach isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incorrect class&quot;</span><span style="color: #002200;">&#41;</span>;
            assertThat<span style="color: #002200;">&#40;</span>coach, equalTo<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>popularCoaches objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// popularCoaches defined elsewhere with expected data</span>
            <span style="color: #002200;">++</span>i;
        <span style="color: #002200;">&#125;</span>
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;per_page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">11</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total_pages&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span> failBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        STFail<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unexpected error returned&quot;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// see http://drewsmitscode.posterous.com/testing-asynchronous-code-in-objective-c</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>loopUntil <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>hasCalledBack <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSRunLoop</span> currentRunLoop<span style="color: #002200;">&#93;</span> runMode<span style="color: #002200;">:</span>NSDefaultRunLoopMode beforeDate<span style="color: #002200;">:</span>loopUntil<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    swizzler <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You can use any class to provide an implementation of the replacement method. This approach allows you can return various responses from your HTTP request. Here is one that returns the expected payload that is read from a file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> RequestPopularCoachesPassMock <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> RequestPopularCoachesPassMock
&nbsp;
<span style="color: #11740a; font-style: italic;">// Swizzled method</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>completeBlock<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>FileReader dataWithContentsOfBundleFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;popularCoaches.json&quot;</span><span style="color: #002200;">&#93;</span>;
        completeBlock<span style="color: #002200;">&#40;</span>data<span style="color: #002200;">&#41;</span>;        
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>In this blog post I have demonstrated how to use the Objective-C runtime dynamic method replacement technique known as *method swizzling* to assist with unit testing in a situation where there is a 3rd party web service that I want to mock. This technique can be used during development to assist with integrating of an iOS app with a web service. It helps speed up development and prototyping. It can also be applied during development in the normal running of your app if you want to mock out portions of or all of a web service.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
