Skip to content

Instantly share code, notes, and snippets.

@CharlesHarley
Created November 11, 2013 11:14
Show Gist options
  • Save CharlesHarley/7411681 to your computer and use it in GitHub Desktop.
Save CharlesHarley/7411681 to your computer and use it in GitHub Desktop.
Category for OCMockObject that adds a verify method that takes a delay. Useful if the code you're testing is asynchronous. Avoid using delays in your tests if you can as it makes them brittle and slow but sometimes you just have no choice.
/**
* The MIT License (MIT)
*
* Copyright (c) 2013 Charles Harley
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <OCMock/OCMockObject.h>
@interface OCMockObject (DelayedVerify)
- (void)verifyWithDelay:(NSTimeInterval)delay;
@end
@implementation OCMockObject (DelayedVerify)
- (void)verifyWithDelay:(NSTimeInterval)delay {
NSTimeInterval i = 0;
while (i < delay) {
@try {
[self verify];
return;
} @catch (NSException *e) {}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
i += 0.5;
}
[self verify];
}
@end
@erikdoe
Copy link

erikdoe commented Mar 13, 2014

Saw the pull request and would be happy to add this to OCMock proper. Just a question regarding the license, Charles. Would you be okay with adding this code to OCMock under its current license (BSD based) and the license OCMock will change to soon (Apache 2)? I know MIT is compatible but for cosmetic reasons I'd prefer not to have to paste the MIT license banner into the OCMock class.

@CharlesHarley
Copy link
Author

Apologies @erikdoe, didn't realise you had posted this comment. The code has already been merged but for the record I am happy for the license to change to Apache 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment