OCUnit & NSBundle

10,347

Found only one solution for this problem.

When I build my unit-tests, the main bundle's path is not equal to my project's bundle (created .app file). Moreover, it's not equal to LogicTests bundle (created LogicTests.octest file).

Main bundle for unit-tests is something like /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin. And that's why program can't find necessary resources.

And the final solution is to get direct bundles:

NSString *path = [[NSBundle bundleForClass:[myClass class]] pathForResource:name ofType:@"png"];

instead of

NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
Share:
10,347
kpower
Author by

kpower

iOS (Objective-C, Swift), Android (Java, Kotlin) & Web (PHP, Symfony framework). Of course can make client-server projects using stack mentioned before. Feel free to contact me at: kpower.public[dog]gmail.com

Updated on June 04, 2022

Comments

  • kpower
    kpower about 2 years

    I created OCUnit test in concordance with "iPhone Development Guide". Here is the class I want to test:

    // myClass.h
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface myClass : NSObject {
        UIImage *image;
    }
    @property (readonly) UIImage *image;
    - (id)initWithIndex:(NSUInteger)aIndex;
    @end
    
    
    // myClass.m
    #import "myClass.m"
    
    @implementation myClass
    
    @synthesize image;
    
    - (id)init {
        return [self initWithIndex:0];
    }
    
    - (id)initWithIndex:(NSUInteger)aIndex {
        if ((self = [super init])) {
            NSString *name = [[NSString alloc] initWithFormat:@"image_%i", aIndex];
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
            image = [[UIImage alloc] initWithContentsOfFile:path];
            if (nil == image) {
                @throw [NSException exceptionWithName:@"imageNotFound"
                    reason:[NSString stringWithFormat:@"Image (%@) with path \"%@\" for current index (%i) wasn't found.",
                        [name autorelease], path, aIndex]
                    userInfo:nil];
            }
            [name release];
        }
        return self;
    }
    
    - (void)dealloc {
        [image release];
        [super dealloc];
    }
    
    @end
    

    And my unit-test (LogicTests target):

    // myLogic.m
    #import <SenTestingKit/SenTestingKit.h>
    #import <UIKit/UIKit.h>
    #import "myClass.h"
    
    @interface myLogic : SenTestCase {
    }
    - (void)testTemp;
    @end
    
    @implementation myLogic
    
    - (void)testTemp {
        STAssertNoThrow([[myClass alloc] initWithIndex:0], "myClass initialization error");
    }
    
    @end
    

    All necessary frameworks, "myClass.m" and images added to target. But on build I have an error:

    [[myClass alloc] initWithIndex:0] raised Image (image_0) with path \"(null)\" for current index (0) wasn't found.. myClass initialization error

    This code (initialization) works fine in application itself (main target) and later displays correct image. I've also checked my project folder (build/Debug-iphonesimulator/LogicTests.octest/) - there are LogicTests, Info.plist and necessary image files (image_0.png is one of them).

    What's wrong?

  • Macarse
    Macarse almost 13 years
    Thanks for your answer. Using [self class] is also possible, leaving that line like this: NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:@"png"];
  • kpower
    kpower almost 13 years
    As far as I know, when you develop standard iPhone application, the only bundle is used to store all your sources & resources. So, theoretically, any "custom" (created by yourself) class can be used here. But I didn't check this idea.
  • aryaxt
    aryaxt over 12 years
    Thanks a lot, I've been looking for this answer for the past 3 days
  • John Rogers
    John Rogers over 8 years
    After struggling with this for quite a while, this answer was a god send. Though almost 6 years old, it is still a great solution for those like me wanting to unit test static libraries that include bundles.