如果我有一个对象使用 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