Test-Driven Development

Now that we have a good handle on how to create a unit test, we are going to delve into the realm of test-driven development (TDD). TDD, in a nutshell, is figuring out the least number of objects you need to create in order to get your application to work the way you want it to. TDD utilizes the idea that you will write your tests first rather than after you have already completed your application.

If we write tests for the app now, we’ll just be checking functionality we already know works. In TDD, we write the test first, fail for lack of any working functionality, then press ahead and actually create the functionality.

Why do we want to do all this extra work before we write a line of code? Let’s jump in the Way Back machine and visit your elementary school English class. Remember back when you were learning how to write stories your teacher told you to write an outline. We write outlines for our stories so that we have an idea about how our story is going to go. We want to figure out the beginning, middle, and end so that we can write a tight and cohesive story that follows a path and has an ending that makes sense. If you go into a story not sure about what is going happen, you’ll wind up writing lots and lots of plot where nothing happens.

Our time is valuable. It is in our best interest to figure out exactly which features are important and which ones are not before you spend a week trying to figure out and debug a feature that we figure out later doesn’t fit in with what we want our app to do.

So let’s add a new feature, TDD style. Let’s say we want to have the web view load itself when the app starts up, without having to tap Show My Tweets. We’ll start by writing a test to make sure the web view got populated, initially failing because it’s not being populated, then go back and add the feature. When the test passes, our feature is good to go.

images/testing/xcode-new-file-targets.png

We’ll start by creating a new test class. Before you create this class, click on the PragmaticTweetsTests group. Use the menu item File > New > File to bring up a template of file types. Choose iOS Source from the left pane, and on the right, select the Unit Test Case Class template. Name our new class WebViewTests. Make sure this class is a subclass of XCTestCase and that it is attached to the PragmaticTweetsTests target before creating the class, as shown in the figure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.7