I have been writing tests around my iPhone apps’ view controllers in order to follow the same TDD practices we use in other environments. Writing tests first has changed the way I structure my code in a couple of ways which I think offer immediate and emergent benefits for my applications. Most of an iPhone application’s business logic is implemented in its view controllers. Testing those controllers is therefore a priority if I want to have a well tested application.
Below are some examples of the sort of tests I have written for my view controllers using GTM, Hamcrest, and OCMock (our iPhone Unit Testing Toolkit). Hopefully this can serve as a starting point for the tests you could be writing for your own projects.
Continue reading ‘Testing View Controllers’
Author Archive for jonah
I’ve been following a Rails convention and defining multiple environments for my iPhone projects. This allows me to quickly switch application wide settings when running a test, development, or production version of the app.
Continue reading ‘Multiple Environments in an iPhone Project’
I’m working on a application which is deployed to Engine Yard’s Cloud infrastructure and I wanted to automatically redeploy the application whenever our tests passed on our continuous integration server.
Engine Yard will eventually allow us to push a branch to our cloud environment from git (ie “git push engineyard master”) but until that is available the best option seems to be to trigger a github post receive hook directly.
To that end I rewrote an existing script to trigger post receive hooks as a rake task; http://gist.github.com/271433
Continue reading ‘Automatically deploying to Engine Yard Cloud’
My current rails project is using Blue Ridge and the tool set it bundles together (Rhino, env.js, Screw.Unit, and Smoke) to write test driven JavaScript. The ability to write tests of my JavaScript, in JavaScript, as efficiently as I can test other languages has forced me to rethink how I structure my work and I’m writing better code because of it.
There are a couple of different options for JS testing available at this point but Blue Ridge’s plugin is the first I have seen which made writing test driven code as easy as I have come to expect while still providing all the functionality I need. In particular;
- Running my tests in a browser is fast. Refreshing fixture pages to rerun tests is dramatically more efficient than waiting for selenium tests to cycle.
- I can debug my tests as they run against their fixtures pages and inspect or manipulate the DOM at any point.
- I have mock and fake objects available to test behavior and stub out dependencies.
- My JS tests run in continuous integration, just like all the other tests.
By writing fixtures pages and tests first I am immediately using all of my code in two locations and running it against slightly different DOMs. That has pushed me to encapsulate most of my code as jQuery plugins and think carefully about how much I depend on the page’s structure. I end up with terse selectors instead of using whatever attributes are readily available and a clear configuration point for my page’s behavior as I chain together plugin calls in $(document).ready().
We’d like to see what other developers have done and share what we have learned so far so Carbon Five is hosting an open discussion of JavaScript testing in our office this week.
Topic: Javascript Testing
When: Tuesday Nov 17
6pm: arrive/network
6:30 – 8 presentation and discussion
Where: Carbon Five office: 171 2nd Street, 4th Floor
Please RSVP if you would like to attend.
Working in Xcode I almost forgot how much I like IntelliJ’s code formatting tools. Luckily for me Mike Smith recently pointed out thatĀ UniversalIndentGUI can be used to setup your own code formatting in Xcode.
Continue reading ‘Code Formatting in Xcode’
Most of my iPhone apps need to accept keyboard input somewhere and that means I need to reposition my UI elements such that the keyboard does not hide the field the user is typing in. The iPhone Application Programming Guide offers one example of Moving Content That Is Located Under The Keyboard but this is not necessarily the best solution.
Continue reading ‘Resizing views around the keyboard’
While working on iPhone applications I have found it useful to load as much of the UI layout and styling from nib files as possible. As a result I often want to load the cells for a table view from a nib file and created a factory class to handle this behavior every time I want to reuse it.
When I need to use a custom cell in a table view I add a cell factory as a property on the view’s controller and select a cell to use by setting that property when constructing the controller. This often allows me to reuse a single factory across multiple controllers so I don’t have to repeat myself by including duplicate references to the same cell nib in every controller.
I recently needed to support some basic gesture recognition in an iPhone app. As it turns out this is a little bit tricky to do when the touches occur within the same responder chain as a view which already responds to touches (like a UIScrollView or UIWebView which might need to scroll or zoom in addition to handling my custom gestures).
Continue reading ‘iPhone gesture recognition’
UILabels are often the most convenient way to display simple text in an iPhone app however I often want a label to display variable text which may need to wrap to multiple lines. Simply increasing the height of the UILabel doesn’t help because a single line of text will be centered vertically within the label’s bounds. Instead I need to computer the size of the text I want to render and update the label’s frame’s height accordingly.
In most cases I want to adjust the size of not only a label but the view containing that label as well so I wrote a category to resize labels and their parent views.
Previously we discussed building anĀ iPhone Unit Testing Toolkit. I wanted to reuse that work without having to repeat those configuration steps every time I started a new Xcode project. Xcode provides a number of templates or starting iPhone applications so I made my own versions which include all of the unit testing tools we rely on.
Continue reading ‘Custom Xcode Templates for iPhone Development’

