Kishor Kundan
Kishor Kundan

Reputation: 3165

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");
}

Upvotes: 0

Views: 483

Answers (1)

Jon Reid
Jon Reid

Reputation: 20980

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?

Upvotes: 2

Related Questions