OCMock与Core数据动态属性问题 [英] OCMock with Core Data dynamic properties problem

查看:135
本文介绍了OCMock与Core数据动态属性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OCMock模拟一些Core Data对象。以前,我有使用Objective-C 1.0样式显式访问器实现的属性:

I'm using OCMock to mock some Core Data objects. Previously, I had the properties implemented with Objective-C 1.0 style explicit accessors:

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

// -- implementation provides generated implementations for both getter and setter

现在我把代码移动到Objective-C 2.0,想要利用新的@property语法和Core Data对象的动态生成的方法实现:

Now I've moved the code to Objective-C 2.0 and want to take advantage of the new @property syntax, and the dynamically-generated method implementations for Core Data objects:

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

// -- Core Data object implementation
@implementation MyItem
@dynamic PDFName;
@end

但是,现在当我创建一个mock项目,处理动态属性:

However, now when I create a mock item, it doesn't seem to handle the dynamic properties:

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

错误如下所示:

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!

我做错了什么?有没有另一种方法来模拟一个核心数据/对象与@dynamic行动?

Am doing something wrong? Is there another way to mock a Core Data / object with @dynamic prooperties?

推荐答案

OCMock论坛 a>

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

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

Basically he suggests abstracting out your Core Data object's interface to a protocol, and using that protocol instead of the class where you pass instances of your core data object around.

我为我的核心数据对象这样做。那么你可以使用mockForProtocol:

I do this for my core data objects. Then you can use mockForProtocol:

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

工作太棒了!他还建议创建一个接口的非核心数据模拟实现,只是合成属性:

Works great! He also suggests creating a non-core data mock implementation of the interface which just synthesizes the properties:

@implementation MockMyItem
@synthesize PDFName;
@end

...

id <MyItemInterface> myItemStub = [[MockMyItem alloc] init] autorelease];
[myItem setPDFName:@"fakepath.pdf"];

我也使用过这个,但我不知道它添加任何东西mockForProtocol: / stub:方法,这是另一回事。

I've used this as well, but I'm not sure it adds anything over the mockForProtocol:/stub: approach, and it's one more thing to maintain.

这篇关于OCMock与Core数据动态属性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆