Previous Entry Share Next Entry
Trust, but verify.
latest
chanson
President Reagan, for all his faults, gave us a very useful aphorism in describing his approach to diplomacy with the Soviet Union: "Trust, but verify." This is also a very useful approach to take when writing unit tests when you're working with a framework, particularly when you're developing a human interface.

For example, Cocoa uses the target-action pattern for controls. When you create an NSButton you can specify which object to send a message to when it's clicked — the target — and what message to send — the action.

You can trust that when you click the button, Cocoa will cause the action message to be sent to the target object so long as they have been properly specified. Therefore you probably don't need to write a unit test that simulates clicking on the button. However, you should verify that your button has had its target and action properly specified, and you can write a test for this.

You can also apply this principal in your own code. Let's say you're implementing a new type of control that also has to follow the target-action pattern. In the tests for your control itself, you probably will want to simulate the appropriate user-interface events and see that a testing instance of your control behaves appropriately. However, you don't need to do this in the code that uses the control — you can trust that the control behaves correctly due to the tests you wrote for the control itself, and just verify that it's been properly configured.

testing this one...

(Anonymous)

2007-11-21 08:26 pm (UTC)

thanks for the GREAT post! Very useful...

I've been thinking about doing exactly this but I'm unsure how. I'm still spinning up on unit testing in cocoa (such a PITA in comparison) and I don't have all the tools in my toolbox that I normally have. Could I bother you for a simple example of verifying the a UI binding?

ETA: Nevermind. I found everything (I think) I need via http://chanson.livejournal.com/tag/unit+testing

Edited at 2009-10-27 02:30 am (UTC)