OCHamcrest
OCHamcrest

Contents:

What is OCHamcrest?

OCHamcrest is:

Matchers are useful for a variety of purposes, such as UI validation. But they're most commonly used for writing unit tests that are expressive and flexible.

OCHamcrest is used for both Mac and iOS development with:

How do I add OCHamcrest to my project?

Rather than build OCHamcrest yourself, I recommend you use the pre-built release available in Downloads. This works for projects using Automatic Reference Counting (ARC) as well as for projects using traditional memory management.

(But if you still want to build OCHamcrest yourself, go into the Source folder and execute MakeDistribution.sh from Terminal.)

The steps vary slightly depending on whether your project is a Mac project or an iOS project:

Mac:

Note: If your Console shows

otest[57510:203] *** NSTask: Task create for path '...' failed: 22, "Invalid argument".  Terminating temporary process.

double-check your Copy Files phase.

iOS:

My first OCHamcrest test

We'll start by writing a very simple Xcode unit test, but instead of using OCUnit's STAssertEqualObjects function, we'll use OCHamcrest's assertThat construct and a predefined matcher:

#import <SenTestingKit/SenTestingKit.h>

#define HC_SHORTHAND
#import <OCHamcrest/OCHamcrest.h>

@interface BiscuitTest : SenTestCase
@end

@implementation BiscuitTest

- (void)testEquals
{
    Biscuit* theBiscuit = [Biscuit biscuitNamed:@"Ginger"];
    Biscuit* myBiscuit = [Biscuit biscuitNamed:@"Ginger"];
    assertThat(theBiscuit, equalTo(myBiscuit));
}

@end

The assertThat function is a stylized sentence for making a test assertion. In this example, the subject of the assertion is the object theBiscuit, which is the first method parameter. The second method parameter is a matcher for Biscuit objects, here a matcher that checks one object is equal to another using the -isEqual: method. The test passes since the Biscuit class defines an -isEqual: method.

OCHamcrest's functions are actually declared with an "HC" package prefix (such as HC_assertThat and HC_equalTo) to avoid name clashes. To make test writing faster and test code more legible, shorthand macros are provided if HC_SHORTHAND is defined before including the OCHamcrest header. For example, instead of writing HC_assertThat, simply write assertThat.

Predefined matchers

OCHamcrest comes with a library of useful matchers:

The arguments for many of these matchers accept not just a matching value, but another matcher, so matchers can be composed for greater flexibility. For example, only_contains(endsWith(@".")) will match any collection where every item is a string ending with period.

Syntactic sugar

OCHamcrest strives to make your tests as readable as possible. For example, the is matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. The following assertions are all equivalent:

assertThat(theBiscuit, equalTo(myBiscuit));
assertThat(theBiscuit, is(equalTo(myBiscuit)));
assertThat(theBiscuit, is(myBiscuit));

Writing custom matchers

A key feature of OCHamcrest is its extensibility. See Writing custom matchers for an example of how to write your own matchers.

More resources