iosunit-testingocmockito

OCMockito: verify that a method of a mock is called sometime in the future


First I mock an object. Then I do something that should make the object's certain method be called. The call is asynchronous.

So what I'd like to verify is: in at most 5 seconds, this method of the mock object should be called.

Any idea?


Solution

  • OCMockito doesn't support asynchronous verification (yet). Until it does, I'd recommend using a hand-rolled mock instead, and OCHamcrest's assertWithTimeout.

    For example, here's a hand-rolled mock to confirm that fooBar was called:

    @interface MockFoo : NSObject
    @property (nonatomic, assign) BOOL fooBarWasCalled;
    - (void)fooBar;
    @end
    
    @implementation MockFoo
    
    - (void)fooBar
    {
        self.fooBarWasCalled = YES;
    }
    
    @end
    

    Then with OCHamcrest:

    assertWithTimeout(5, thatEventually(@(myMock.fooBarWasCalled), isTrue());