Quality Coding

Category Archives for iOS Tools

Emoji: House

How OCHamcrest’s “hasProperty” Can Make Your Tests Simpler


Whenever test code has repeated patterns, there’s an opportunity to make it more expressive and readable. This can sometimes be done by creating a new OCHamcrest matcher.

OCHamcrest features a great new matcher submitted by Justin Shacklette: hasProperty.

“hasProperty” is something of a misnomer, because it’s not just for properties. Any method, without arguments, that returns an object, is fair game. But I couldn’t think of a better name; I think it communicates well. Let’s take a look at a before-and-after code example.

Continue reading

Emoji: Pig Face

Hamcrest: A Tool for Readable, Powerful Unit Tests


Unit test code is often tricky to read. Test output is often hard to read. There’s got to be a better way.

There is!

Hamcrest is an important tool for writing unit tests. It’s now a standard feature of jUnit, and lets you build test expressions that are expressive and readable:

  • Readable source code
  • Readable diagnostic output

I’ve ported Hamcrest to two languages:

Since Quality Coding focuses on iOS development, I’ll write about OCHamcrest on this blog.

…But Justin Shacklette beat me to the punch! His post Intro to OCHamcrest is a nice overview. He introduces the many “matchers,”  illustrating them with code samples. He even makes special notes to call out areas that might trip you up.

I’ll eventually write about OCHamcrest myself. Until then, go check out Justin’s writeup.

Any questions about OCHamcrest? Leave a comment below.

See also: How OCHamcrest’s “hasProperty” Can Make Your Tests Simpler