Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
/ LVForgivingAssert Public archive

Create unit tests for methods that use NSAssert as pre-condition check.

License

Notifications You must be signed in to change notification settings

lovoo/LVForgivingAssert

Repository files navigation

LVForgivingAssert

CI Status Carthage compatible CocoaPods Compatible CocoaPods

What does it do?

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;
[self aMethodWithNonnullArgument: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([self aMethodWithNonnullArgument:string]);

With LVForgivingAssert it’s also possible to test the method without having NSAssert enabled while testing in debug mode:

LVIgnoreAssert(^{
    XCTAssertNil([self aMethodWithNonnullArgument:string]);
});

How does it work?

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.

Example

To run the example project, clone the repo, and run pod 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.

Requirements

Installation

LVForgivingAssert is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "LVForgivingAssert"

Author

Benjamin Herzog, mail@benchr.de

License

See the LICENSE file for more info.