<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Unit Testing and Mocking for iPhone Apps</title>
    <link>http://blog.lathi.net/articles/2009/02/06/unit-testing-and-mocking-for-iphone-apps</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Unit Testing and Mocking for iPhone Apps</title>
      <description>&lt;p&gt;This was so much harder than I expected it to be.  I&amp;#8217;m new to Objective-C, but a long time &lt;span class="caps"&gt;TDD&lt;/span&gt;-er with Ruby.  Being able to write unit tests and mocks is an integral part of any language I work with now.  After much searching and trial and error I got it to work.&lt;/p&gt;


	&lt;p&gt;First, starting with a new iPhone application I followed &lt;a href="http://www.sente.ch/s/?p=535&amp;#38;lang=en"&gt;these directions&lt;/a&gt; on how to get it setup.  The &lt;a href="http://www.sente.ch/"&gt;Sen:Te&lt;/a&gt; guys are who wrote OCUnit in the first place.  There are other directions out there, but don&amp;#8217;t let them lead you astray.  XCode 3 doesn&amp;#8217;t support unit testing iPhone applications out of the box.  Most of the directions for setting up unit testing is for straight Mac &lt;span class="caps"&gt;OS X&lt;/span&gt; Cocoa apps and not iPhone apps.  The gist of the directions are:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Add a new target based on Mac &lt;span class="caps"&gt;OS X&lt;/span&gt; Cocoa Unit Test Bundle.&lt;/li&gt;
		&lt;li&gt;In the new test target&amp;#8217;s inspector window, whack all the &amp;#8220;User-Defined Settings&amp;#8221; in the new build tab&amp;#8217;s settings.&lt;/li&gt;
		&lt;li&gt;Also delete the &amp;#8216;&#8216;Other Linker Flags&amp;#8217; setting.&lt;/li&gt;
		&lt;li&gt;In the General tab, add a dependency to the &lt;code&gt;SenTestingKit.framework&lt;/code&gt;.  Here&amp;#8217;s where it gets tricky.  Don&amp;#8217;t just add any &lt;code&gt;SenTestingKit.framework&lt;/code&gt;.  You need to add the one from:&lt;/li&gt;
	&lt;/ul&gt;


&lt;code&gt;
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/Developer/Library/Frameworks/SenTestingKit.framework
&lt;/code&gt;

 * Now open the inspector for your application&amp;#8217;s main target and set a direct dependency between it and the test target.  That will make the tests build and run every time you build the app.  Test failure causes a build failure for your main target.

	&lt;p&gt;I also added a &amp;#8220;Unit Tests&amp;#8221; group/folder to the project as a place to store my tests separate from my classes.  When you want to create a new test, you simply add a new file and choose the &amp;#8220;Objective-C test case class&amp;#8221; template.  Make sure you add it to the test target and not your main application target.&lt;/p&gt;


	&lt;p&gt;So that gives you unit tests so you can make assertions; but that&amp;#8217;s about it.  You also need &lt;a href="http://www.mulle-kybernetik.com/software/OCMock/"&gt;OCMock&lt;/a&gt;.  I had a little trouble getting this installed and working, but following &lt;a href="http://mitchellhashimoto.com/using-ocmock-with-xcode-3-in-leopard/"&gt;Mitchell Hasimoto&amp;#8217;s&lt;/a&gt; notes I was able to get it working.  Download the OCMock framework and save it into &lt;code&gt;/Library/Frameworks&lt;/code&gt;.  Then in you test target&amp;#8217;s inspector add it as a dependency.  That should be enough.  Here&amp;#8217;s a little test class that I used to prove to myself it was working:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;testTruth.h&lt;/code&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#import &amp;lt;SenTestingKit/SenTestingKit.h&amp;gt;

@interface testTruth : SenTestCase {

}

- (void) testTruth;
- (void)testAcceptsStubbedMethod;

@end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;code&gt;TestTruth.m&lt;/code&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#import &amp;quot;testTruth.h&amp;quot;
#import &amp;lt;OCMock/OCMock.h&amp;gt;

@implementation testTruth

- (void) testTruth {
    STAssertTrue(1 == 1, @&amp;quot;Must be true&amp;quot;);
}

- (void)testAcceptsStubbedMethod {
    id mock = [OCMockObject mockForClass:[NSString class]];
    [[mock stub] lowercaseString];
    [mock lowercaseString];
}
@end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 06 Feb 2009 14:54:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:9676eda5-d732-487f-b069-37f7f4d3b21c</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2009/02/06/unit-testing-and-mocking-for-iphone-apps</link>
      <category>Programming</category>
      <category>Test Driven Development</category>
      <category>iPhone</category>
      <category>testing</category>
      <category>mocks</category>
    </item>
  </channel>
</rss>
