简体   繁体   中英

NSBundle while unit testing with OCUnit returns (null)

Problem : Retrieving from NSBundle returns (null) while running Unit Test, returns valid object on run time.

What have I done ?

I have searched SO with similar problems, apple's developer and other online resources for this problem but with no luck.

I have tried solution from SO questions; OCUnit & NSBundle , NSBundle pathForResource returns nil .

All solutions I went through online point out to one thing Bundle for run time and tests are different.

All of them recommend having

[NSBundle bundleForClass: [self class]];

instead of

[NSBundle mainBundle];    

Question : I don't understand if the above fix is with setter injection or in the source file itself. ? Is there any other way I can test the method getIpAdress ?

Code

Class that returns (null),

// iRNetworkingObject.m

@implementation iRNetworkingObject

-(NSString*) getIpAdress {
     NSDictionary *infoDict = [[NSBundle bundleForClass:[self class]] infoDictionary];
     NSString *lookUpAdress = [infoDict objectForKey:@"LookUpIpAdress"];
     return lookUpAdress;
}
@end

Test class

- (void) testGetIpAdress {

    iRNetworkingObject* networkingObject = [[iRNetworkingObject alloc] init];
    NSString* testCase = @"192.168.2.1";
    NSString* encondedString = [networkingObject getIpAdress]];

    STAssertTrue([testCase isEqualToString:encondedString], @"Not equal");
}

bundleForClass: is important in test code, to make sure you get the application bundle instead of the test bundle. You shouldn't have to do that in production code.

Your key @"LookUpIpAdress" is misspelled. Is that the problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM