erikdoe / ocmock

Mock objects for Objective-C
http://ocmock.org
Apache License 2.0
2.16k stars 606 forks source link

Wait for the N-times call the method. #303

Closed soranoba closed 4 years ago

soranoba commented 8 years ago

I want to do the following:

OCMExpect([mock someMethod]).onReturn(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [mock someMethod];
});

OCMVerifyWithDelay(mock, 1.0f);
OCMVerifyWithDelay(mock, 1.0f); // success

This way that multiple times run OCMVerifyWithDelay is not correctly work.

id observerMock = OCMObserverMock();
[notificatonCenter addMockObserver:aMock name:SomeNotification object:nil];
[[observerMock expect] notificationWithName:SomeNotification object:[OCMArg any]];

[mock  someMethod];
[mock  someMethod]; // failed

On other hand, this way isn't correctly work. Here exception is thrown.

I as a solution, I hope added the option that remove the exception.

if (! throughEnabled) {
    [NSException raise:NSInternalInconsistencyException format:@"%@: unexpected notification observed: %@", [self description], 
    [aNotification description]];
}
carllindberg commented 8 years ago

You should be able to add a OCMStub call after the OCMExpect (or N calls to expect), so that additional calls won't throw exceptions.

soranoba commented 8 years ago

@carllindberg I have questioned the exception that is thrown observerMock.

If we don't use observerMock and multiple calls to the stub and expect, exception won't throw. However, in the way we need to do to alternately the OCMExpect and the method call. This is a difficult when it use async.