How to create a unit test for a method that varies depending on the SDK?

I need to make my iOS library compatible with iOS 6+, so I make it compatible with the libraries available in iOS 7, which make my life easier (which will eventually allow me to delete the old code).

For example, the built-in base64 encoding is available in iOS 7+, so I do a check that looks like this (which I pulled from another SO question):

if([NSData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
     // Do iOS 7 stuff
} else {
     // Break my head over iOS 6 compatibility
}

Everything seems to be working fine, but how do I write unit test (s) to test both situations? If I use the same if-else check in my unit test to defeat the unit test goal, right?

+4
source share
1

, , iOS 6, iOS 7, - . unit test, , CI (Xcode Bots, Travis CI ..) iOS. , , Subliminal, iOS 6 7 iPhone iPad.

Edit:

, base64. , iOS , , . CI iOS 6 7. iOS 7, YES if, iOS 6 NO if.

- (void)testEncoding
{
    NSData *base64EncodedData = [@"aGVsbG8=" dataUsingEncoding:NSUTF8StringEncoding];

    NSString *decodedString = [self decodeData:base64EncodedData];

    XCTAssert([decodedString isEqualToString:@"hello"], @"The base64 encoded string should decode to the word `hello`");
}

- (NSString *)decodeData:(NSData *)data
{
    if ([data respondsToSelector:@selector(base64EncodedDataWithOptions:)]) {

        return [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedData:data options:0]
                                     encoding:NSUTF8StringEncoding];
    } else {
        // Whatever method you use on iOS 6 to decode the base 64 data.
        return nil;
    }
}
+1

Source: https://habr.com/ru/post/1540310/


All Articles