There are many different unit testing frameworks available for the iOS platform, and OCUnit included within Xcode is terrific for most cases.
Today I came across a great library for anyone working with OCUnit (and several other frameworks) providing a library of matchers for building test expressions covering many situations saving you a lot of time, and providing a framework to make it easier to create your own test expressions.
Hamcrest works with the iOS Unit testing frameworks OCUnit, Google Toolbox for Mac, GHUnit, Cedar, and OCMock , and OCHandMock.
Here’s a list of the predefined matchers from the Hamcrest wiki:
- Object
- conformsTo – match object that conforms to protocol
- equalTo – match equal object
- hasDescription – match object’s -description
- hasProperty – match return value of method with given name
- instanceOf – match object type
- nilValue, notNilValue – match nil, or not nil
- sameInstance – match same object
- Number
- closeTo – match number close to a given value
- equalTo<TypeName> – match number equal to a primitive number (such as equalToInt for an int)
- greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo – match numeric ordering
- Text
- containsString – match part of a string
- endsWith – match the end of a string
- equalToIgnoringCase – match the complete string but ignore case
- equalToIgnoringWhitespace – match the complete string but ignore extra whitespace
- startsWith – match the beginning of a string
- stringContainsInOrder – match parts of a string, in relative order
- Logical
- allOf – “and” together all matchers
- anyOf – “or” together all matchers
- anything – match anything (useful in composite matchers when you don’t care about a particular value)
- isNot – negate the matcher
- Collection
- contains – exactly match the entire collection
- containsInAnyOrder – match the entire collection, but in any order
- empty – match empty collection
- hasCount – match number of elements against another matcher
- hasCountOf – match collection with given number of elements
- hasEntries – match dictionary with list of key-value pairs
- hasEntry – match dictionary containing a key-value pair
- hasItem – match if given item appears in the collection
- hasItems – match if all given items appear in the collection, in any order
- hasKey – match dictionary with a key
- hasValue – match dictionary with a value
- onlyContains – match if collections’s items appear in given list
- Decorator
- describedAs – give the matcher a custom failure description
- is – decorator to improve readability – see Syntactic sugar below
You can find Hamcrest on Google code here and the Objective-C wiki can be found here.
A nice time saver for building unit tests.
Original article: Objective-C Library Of Matcher Objects For OCUnit, GHUnit, Cedar, More For Easier Testing
©2012 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.