Posted in Coding
For a few releases the Apple development tools have included OCUnit and many developers have now started to write unit tests. There are lots of tutorials that explain how this is done for the straight-forward cases but there's one area of testing that has proven difficult on most platforms, and that is testing of the user interface. That said, there are a few things that make this an easier problem to solve with Cocoa and in this post I'll explain why.
Let's look at an example. CCMenu is a small application that displays the status of CruiseControl continuous integration servers. As part of adding a new project to be monitored the user has to enter the URL for the CruiseControl server and a combox provides a history of previously used servers.
The same dialog has a matrix of buttons to set the type of the server. Actually, by default CCMenu detects the server type automatically but that's beside the point. What we're interested in is the piece of functionality that is responsible for selecting the correct server type for the URL chosen by the user. In our case, this would be Cruise Control Dashboard.
One difference between Cocoa and most other UI frameworks is the fact that the user interface is stored by serialising the objects, rather than generating code. For this reason I'm happy to not insist on test-first for the actual interface. What I do want to test is the code in the controller classes.
Obviously, I could load the serialised objects, locate the elements involved and then use methods such as performClick: to trigger the actions. Sounds convoluted? Yes, I agree. Luckily there's a better way.
This is a case where testing is tricky because the object under test interacts with objects that are difficult to deal with. In such cases dynamic mock objects have proven extremely useful. A good introduction can be found here and mockobject.com lists papers and implementations. For Objective-C I created OCMock. Let's look at how this helps us testing our controllers.
Here are the relevant parts of the interface declaration of the controller. Given that I'm doing test-driven development there's currently no implementation of the methods. I simply created the interface of the controller based on my needs while designing the dialog.
In my test I want to use mock objects instead of loading the actual user interface. So, in the test setup, after creating my controller, I create appropriate mock objects and set up the controller to use these.
You notice that I'm using key-value coding to set the variables (outlets). I'm doing this because the variables are not public and at the same time I don't want to write accessor methods for them.
Now, we can start writing the actual test for the functionality. Have a look at the full test first.
What's going on here? Firstly, I tell the mock object that stands in for the actual combox box that it should stub the stringValue method. This means that when somebody invokes stringValue on this object it will return the string @"http://localhost/cctray.xml". This is all we'd have done with the real combo box anyway.
Secondly, I tell the mock that stands in for the server type matrix that it should expect that the method selectCellWithTag: is called with CCMCruiseControlDashboard as an argument.
Lastly, I invoke the method I want to test. What happens, once the implementation is complete, is that the code will go the the combo box and ask it for its string value. The first mock will return the stubbed value. Now, the code should do whatever it needs to do to figure out which server type this corresponds to and then set that in the server type matrix by selecting the cell with the appropriate tag, and this is what we've told the second mock to expect.
Wait, you might say, we don't have an implementation yet. So, how does this test fail? It doesn't yet. We'll have to tell the mock objects to verify that everything we told them to expect actually occurred, and a logical place for this is the test tearDown.
Strictly speaking, we don't have to verify the combo box mock because it doesn't have any expectations but it's good practice to verify all mocks in the tear down, especially if the same mocks are used for multiple tests. By the way, by default the mocks also have fail-fast behaviour; when they receive a method that wasn't stubbed or expected, they raise an exception right away. Detecting something that wasn't expected can be done right way, detecting that something that was expected didn't occur must be trigger by the user.
Now, if we run this test with an empty implementation of historyURLSelected: it will fail when we tell the server type matrix mock to verify because the expected method hasn't been called. The error message will look something like this:
Unknown.m:0: error: -[CCMPreferencesControllerTest testSelectsServerTypeWhenHistoryURLIsSelected] : OCMockObject[NSMatrix]: expected method was not invoked: selectCellWithTag:0
Adding an implementation like the following one adds the right functionality and makes our test pass.
In summary, testing controllers becomes relatively easy when we follow this pattern:
1. Replace all UI elements with mocks; using key-value coding to access the outlets.
2. Set up stubs with return values for UI elements that the controller will query.
3. Set up expectations for UI elements that the controller should manipulate.
4. Invoke the method in the controller.
5. Verify the expectations.
I find tests following this pattern easier to write and understand than tests that load a NIB file and interact with the actual user interface elements.