The previous article briefly introduced the more common unit test framework in the Ocunit and Ghunit two iOS development, and this article further introduced another weapon in unit testing-matching engine (Matcher Engine). The matching engine can replace the assertion method, with the unit test engine, and the test cases can be more diverse and detailed.
The traditional assertion provides a limited number of methods and functions, taking the two framework mentioned in the guide as an example, even the assertion of relatively rich Ghunit provides only 38 assertions, the scope of which covers only a few aspects of logical comparisons, exceptions and errors, still very single. The possibility of using a matching engine instead of an assertion is much richer, except for the rules supported by common assertions, where the general engine also defaults to include, interval, and inheritance relationships. More importantly, using matching engine developers can develop matching rules on their own, introducing business-related logic judgments.
This article will introduce two matching engine, one is hamcrest Objective-c realize--ochamcrest, the other one is specially for Objective-c/cocoa and born later--expecta. Next, we will combine ghunittest to describe how the two matching engines work in unit tests (ghunittest reference to unit testing in iOS development (i).
Ochamcrest
The introduction of the matching engine must be hamcrest, almost become synonymous with the matching engine. A statement on the home page of the website shows its origins: "Born in Java, Hamcrest now has implementations in a number of languages." The Java-born matching engine now supports Java-only python, Ruby, PHP, Erlang, and Objective-c.
Join the Project
The use of ochamcrest in the iOS project requires first obtaining ochamcrestios.framework, which can be downloaded directly from the quality coding or obtained from the source code compilation on the GitHub. Note: The Ochamcrest project hosted on GitHub is associated with the source code in the form of submodule, so if you use the command line clone project, you need to perform the Git submodule update--init.
Download the source, enter the source directory, execute makedistribution.sh script, will be generated under Source/build/release ochamcrest.framework, Ochamcrestios.framework and OCHamcrest.framework.dSYM, ochamcrestios.framework are the frameworks needed for iOS projects, as shown in Figure 1.
Figure 1, generating ochamcrestios.framework from source code compilation
Open the project that already has the ghunittest installed, and add the ochamcrestios.framework to target in the unit test. In a use case that requires a matching engine, define "Hc_shorthand" and Import "<OCHamcrestIOS/OCHamcrestIOS.h>" (Figure 2).
Figure 2, import the ochamcrestios.framework into the project
Now that the ochamcrest has been installed, you can then use matching rules in the test case to replace the Ghunittest assertion method.