首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OCUnit和NSBundle

OCUnit和NSBundle
EN

Stack Overflow用户
提问于 2010-06-18 11:22:08
回答 1查看 10.3K关注 0票数 39

我根据"iPhone开发指南“创建了OCUnit测试。下面是我要测试的类:

代码语言:javascript
复制
// 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

和我的单元测试(LogicTests目标):

代码语言:javascript
复制
// 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

所有必要的框架,"myClass.m“和图像添加到目标。但在构建时,我有一个错误:

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

这段代码(初始化)在应用程序本身(主目标)中运行良好,稍后会显示正确的图像。我还检查了我的项目文件夹(build/Debug-iphonesimulator/LogicTests.octest/) --里面有LogicTestsInfo.plist和必要的图像文件(image_0.png就是其中之一)。

怎么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-21 12:04:40

只找到了解决此问题的一种方法。

当我构建单元测试时,主包的路径不等于我的项目包(创建的.app文件)的路径。此外,它不等于LogicTests包(创建的LogicTests.octest文件)。

单元测试的主包类似于/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin。这就是为什么程序找不到必要的资源。

最终的解决方案是获得直接捆绑包:

代码语言:javascript
复制
NSString *path = [[NSBundle bundleForClass:[myClass class]] pathForResource:name ofType:@"png"];

而不是

代码语言:javascript
复制
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
票数 128
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3067015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档