4
如果我有一個使用國際志願者組織到某個對象,觀察一個屬性,然後創建該觀察者我不再收到任何通知的部分模仿的對象。爲什麼是這樣?爲什麼OCMock partialMock打破KVO?
這裏有一個小例子:
@interface TestPartialMockAndKVO : SenTestCase
@end
@implementation TestPartialMockAndKVO
- (void)test {
// Should print "Changed!" when foo property is changed
MyObserver* myObserver = [[[MyObserver alloc] init] autorelease];
// But with this line, there is no print out
[OCMockObject partialMockForObject:myObserver];
[myObserver setFoo:@"change"];
}
@end
-
@interface MyObserver : NSObject
@property (copy) NSString* foo;
@end
@implementation MyObserver
- (id)init {
self = [super init];
[self addObserver:self forKeyPath:@"foo" options:0 context:NULL];
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"Changed!");
}
- (void)dealloc { ... }
@end
很好的答案,但是這真的很糟糕,我現在! – RyanM