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:
- 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
- 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
- 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
- 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
- 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
- describedAs – give the matcher a custom failure description
- is – decorator to improve readability – see Syntactic sugar below
A nice time saver for building unit tests.
Submit A Resource
Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?
You can submit the url here.
The resources we feel will appeal to our readers the most will be posted on the front page.