Presenting an alert is normally a difficult dependency (see Identify Difficult Dependencies). For unit tests, it’s a side effect. For UI tests, it interrupts the flow of control, waiting for input. To make it easier to unit test alerts, I wrote a framework called ViewControllerPresentationSpy.
The framework is a collection of three verifiers. One is to capture alerts and action sheets, which we’ll use here. The other two are to capture how view controllers are presented or dismissed.
Find the latest release by going to its GitHub page[20] and clicking “releases.” Download the versioned zip file, which GitHub shows with a size. Drag ViewControllerPresentationSpy.framework to the open Xcode project, into the AlertTests group, as seen here:
In the “Choose options for adding these files” dialog, select the check box labeled “Copy items if needed” to copy the framework into the project folder. And make sure that “Add to targets“ specifies the AlertTests target only.
For third-party frameworks to work, we often need to copy them to a place where the dynamic linker can find them. This is what we need to do with ViewControllerPresentationSpy. In the Project Navigator on the left, select the Alert project. Then select the AlertTests target. Within that, select the Build Phases tab. Click the + button at the top and select “New Copy Files Phase,” as seen here:
This creates a new Copy Files phase at the end of the list. In its Destination pop-up menu, select “Products Directory,” as you’ll see in the image. Click the + button at the bottom of the new Copy Files phase. In the “Choose items to add:” dialog, select ViewControllerPresentationSpy.framework and click Add. (Don’t select the “Code Sign On Copy” check box because we’re not shipping test code.)
Now the helper framework is ready for compiling against, and is also copied into place for dynamic linking at runtime.