J'ai créé un test OCUnit en accord avec le "iPhone Development Guide". Voici la classe que je veux tester :
// 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
Et mon unit-test (cible LogicTests) :
// 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
Tous les frameworks nécessaires, "myClass.m" et les images ont été ajoutés à la cible. Mais lors de la construction, j'ai une erreur :
[[myClass alloc] initWithIndex:0] raised Image (image_0) with path \"(null)\" for current index (0) wasn't found.. myClass initialization error
Ce code (initialisation) fonctionne bien dans l'application elle-même (cible principale) et affiche ensuite l'image correcte. J'ai également vérifié le dossier de mon projet ( build/Debug-iphonesimulator/LogicTests.octest/
) - il y a LogicTests
, Info.plist
et les fichiers images nécessaires ( image_0.png
est l'un d'entre eux).
Qu'est-ce qu'il y a ?