1

It is not the best thing to do, but I would like to verify that a private method of an object is called, so I create a partial mock and add an expectation on the private method.

Synchronizer * sync = [[Synchronizer alloc] initWithCleanup:YES];

sync = [OCMockObject partialMockForObject:sync];
[[(id)sync expect] cleanupPreviousContents];      

When I run the test, cleanupPreviousContents is not called but the test is still successful. Where is the bug ?

Regards, Quentin

Quentin
  • 1,741
  • 2
  • 18
  • 29

1 Answers1

7

Yes, this is a perfectly valid thing to do. But you need to create a new reference for your partial mock:

Synchronizer * sync = [[Synchronizer alloc] initWithCleanup:YES];

id mockSync = [OCMockObject partialMockForObject:sync];
[[mockSync expect] cleanupPreviousContents];  

... do something

[mockSync verify];

Is cleanupPreviousContents called within your initWithCleanup method? If so, you'll have to structure it a bit differently:

Synchronizer *sync = [Synchronizer alloc];

id mockSync = [OCMockObject partialMockForObject:sync];
[[mockSync expect] cleanupPreviousContents];  

[sync initWithCleanup:YES];

[mockSync verify];
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92