OCUnit& NSBundle [英] OCUnit & NSBundle
问题描述
我根据《 iPhone开发指南》创建了OCUnit测试.这是我要测试的课程:
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
还有我的单元测试(LogicTests目标):
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
所有必需的框架,"myClass.m"和添加到目标的图像.但是在构建时我有一个错误:
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
此代码(初始化)在应用程序本身(主要目标)中正常工作,并在以后显示正确的图像.我还检查了我的项目文件夹(build/Debug-iphonesimulator/LogicTests.octest/
)-有LogicTests
,Info.plist
和必要的图像文件(image_0.png
是其中之一).
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).
怎么了?
推荐答案
仅为此问题找到了一种解决方案.
Found only one solution for this problem.
构建单元测试时,主捆绑包的路径与项目捆绑包(创建的.app文件)不同.而且,它不等于LogicTests捆绑软件(创建的LogicTests.octest
文件).
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).
用于单元测试的主捆绑包类似于/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin
.这就是程序无法找到必要资源的原因.
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"];
代替
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
这篇关于OCUnit& NSBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!