4
KVO를 사용하여 개체의 속성을 관찰 한 다음 해당 관찰자의 모의 부분을 만들면 더 이상 알림을받지 못합니다. 왜 이런거야? 여기 왜 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