Returning custom description and debugDescription values for mock objects

136 Views Asked by At

I frequently use OCMock with the excellent OCHamcrest matchers. But I often encounter test failure messages that aren't as helpful as they could be. This is because the OCHamcrest output relies heavily on the value returned by the "description" method of objects but OCMockObject's default description implementation is very generic.

Is there a recommended way to have mock objects return a custom description?

I've tried stubbing the "description" method on my mock objects but that doesn't seem to work.

As a workaround I've created a OCClassMockObject+Description category extension that adds a setMockDescription: method, but I'm hoping there's a more officially supported solution. (If not, maybe I'll polish mine up and submit a pull request.)

Here are examples of some of the results I'd like to improve...

Example Assertion #1

assertThat(recommendedSong, is(mockSong1));

Failure message:

Expected <OCMockObject[SongBar]>, but was <OCMockObject[SongBar]>

Example Assertion #2

assertThat(playlist.songs, contains(mockSong1, mockSong2, nil));

Failure message:

Expected a collection containing [<OCMockObject[SongBar]>, <OCMockObject[SongBar]>], but item 0: was <OCMockObject[SongBar]>
0

There are 0 best solutions below