- Notifications
You must be signed in to change notification settings - Fork0
Create unit tests for methods that use NSAssert as pre-condition check.
License
lovoo/LVForgivingAssert
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Sometimes it is necessary to have parameters in a method that are not nil. Especially for Swift compatibility there are often _Nonnull arguments added, but it is not possible to prohibit Objective-C callers to call a method with nil. Xcode will warn you when using the nil literal but not, when using a nil named pointer like the following example:
- (NSString *)aMethodWithNonnullArgument:(NSString *_Nonnull)string;
NSString *string;[selfaMethodWithNonnullArgument:string];
The usual approach is to use NSAssert to crash a development version when this occurs. This helps very much during development process and forces the caller to call the method correctly. But most times, it’s not that easy and there needs to be a fallback in production mode when NSAssert does not work.When unit testing this method, the app is in debug mode in the most configurations, so you can only test if the method throws:
NSString *string;XCTAssertThrows([selfaMethodWithNonnullArgument:string]);
With LVForgivingAssert it’s also possible to test the method without having NSAssert enabled while testing in debug mode:
LVIgnoreAssert(^{XCTAssertNil([selfaMethodWithNonnullArgument:string]);});
This very tiny library adds a NSAssertionHandler subclass that is set temporarily as the current assertion handler. It does not enable NSAssert and ignores it instead. After the macromis blocked, the original NSAssertionHandler is set, so it does not effect your app behaviour.See the example for more information.
To run the example project, clone the repo, and runpod install
from the Example directory first. The pod is used in the test target, so be sure to hit cmd+U in Xcode or select the 'Test' menu item.
LVForgivingAssert is available throughCocoaPods. To installit, simply add the following line to your Podfile:
pod"LVForgivingAssert"
Benjamin Herzog,mail@benchr.de
See the LICENSE file for more info.
About
Create unit tests for methods that use NSAssert as pre-condition check.