OCMock与Core Data动态属性问题

amr*_*rox 18 cocoa unit-testing core-data ocmock

我正在使用OCMock来模拟一些Core Data对象.以前,我使用Objective-C 1.0样式显式访问器实现了属性:

// -- Old Core Data object header
@interface MyItem : NSManagedObject {}
- (NSString *) PDFName;
- (void) setPDFName:(NSString *)pdfName;
@end

// -- implementation provides generated implementations for both getter and setter
Run Code Online (Sandbox Code Playgroud)

现在我已将代码移至Objective-C 2.0,并希望利用新的@property语法以及Core Data对象的动态生成方法实现:

// -- New Core Data object header
@interface MyItem : NSManagedObject {}
@property (nonatomic, retain) NSString *PDFName;
@end

// -- Core Data object implementation
@implementation MyItem
@dynamic PDFName;
@end
Run Code Online (Sandbox Code Playgroud)

但是,现在当我创建一个模拟项时,它似乎不处理动态属性:

// -- creating the mock item
id mockItem = [OCMockObject mockForClass:[MyItem class]];
[[[mockItem stub] andReturn:@"fakepath.pdf"] PDFName]; // <-- throws exception here
Run Code Online (Sandbox Code Playgroud)

错误如下所示:

Test Case '-[MyItem_Test testMyItem]' started.
2009-12-09 11:47:39.044 MyApp[82120:903] NSExceptionHandler has recorded the following exception:
NSInvalidArgumentException -- *** -[NSProxy doesNotRecognizeSelector:PDFName] called!
Stack trace: 0x916a4d24 0x92115509 0x97879138 0x978790aa 0x9090cb09 0x97820db6 0x97820982 0x10d97ff 0x10d9834 0x9782005d 0x9781ffc8 0x20103d66 0x20103e8c 0x20103642 0x20107024 0x20103642 0x20107024 0x20103642 0x20105bfe 0x907fead9 0x977e4edb 0x977e2864 0x977e2691 0x90877ad9 0xbf565 0xbf154 0x107715 0x1076c3 0x1082e4 0x89d9b 0x8a1e5 0x894eb 0x907e81c7 0x978019a9 0x978013da 0x907dd094 0x907ea471 0x9478c7bd 0x9478c1b9 0x94784535 0x5ede 0x326a 0x5
Unknown.m:0: error: -[MyItem_Test testMyItem] : *** -[NSProxy doesNotRecognizeSelector:PDFName] called!
Run Code Online (Sandbox Code Playgroud)

我做错了什么?有没有其他方法可以使用@dynamic prooperties模拟核心数据/对象?

Chr*_*lay 20

还回复了您在OCMock论坛上的交叉帖子

查看http://iamleeg.blogspot.com/2009/09/unit-testing-core-data-driven-apps.html.

基本上他建议将Core Data对象的接口抽象出来,并使用该协议而不是传递核心数据对象实例的类.

我为我的核心数据对象执行此操作.然后你可以使用mockForProtocol:

id mockItem = [OCMockObject mockForProtocol:@protocol(MyItemInterface)];
[[[mockItem expect] andReturn:@"fakepath.pdf"] PDFName];
Run Code Online (Sandbox Code Playgroud)

效果很好!他还建议创建一个非核心数据模拟实现的接口,它只是合成属性:

@implementation MockMyItem
@synthesize PDFName;
@end

...

id <MyItemInterface> myItemStub = [[MockMyItem alloc] init] autorelease];
[myItem setPDFName:@"fakepath.pdf"];
Run Code Online (Sandbox Code Playgroud)

我也使用过它,但我不确定它是否会在mockForProtocol:/ stub:方法中添加任何内容,而且还需要维护一件事.


小智 11

上面的回答并没有让我满意,因为我不喜欢为此创建一个协议.所以我发现有一种更简单的方法可以做到这一点.代替

[[[mockItem stub] andReturn:@"fakepath.pdf"] PDFName]; // <-- throws exception here
Run Code Online (Sandbox Code Playgroud)

写吧

[[[mockItem stub] andReturn:@"fakepath.pdf"] valueForKey:@"PDFName"];
Run Code Online (Sandbox Code Playgroud)