I've been trying to mock a simple property on UITextField
:
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
I've tried to mock this 2 ways
1.) using the full blown class method swizzle technique (usually for class based methods I'll do this)
- (void)swapOutTextFieldIsEditing
{
method_exchangeImplementations(
class_getClassMethod([UITextField class], @selector(isEditing)),
class_getClassMethod([self class], @selector(isEditingEnabledMock))
);
}
- (void)resetTextFieldIsEditing
{
method_exchangeImplementations(
class_getClassMethod([self class], @selector(isEditingEnabledMock)),
class_getClassMethod([UITextField class], @selector(isEditing))
);
}
+ (BOOL)isEditingEnabledMock {
return isEditingMockResult;
}
2.) using a custom ocmock helper to create a custom "and return bool" method
- (id)andReturnBool:(BOOL)aValue {
NSValue *wrappedValue = nil;
wrappedValue = [NSValue valueWithBytes:&aValue
objCType:@encode(BOOL)];
return [self andReturnValue:wrappedValue];
}
Sadly both techniques failed and I'm almost sure it's because this is a normal property on UITextField
marked readonly w/ a getter (never seen this before on iOS)
**by failed the following NSLog
always returns (null) -meaning I can't stub this
- (IBAction)next
{
for (int i = 0; i<[self.fieldsArray count]; i++) {
NSLog(@"is this field being edited? %@", [[self.fieldsArray objectAtIndex:i] isEditing]);
}
}
Here is my basic stub syntax (used w/ the above)
id firstField = [OCMockObject niceMockForClass:[UITextField class]];
[[[firstField stub] andReturnBool:YES] isEditing];
Does anyone know of another way to stub this result? If not what about the 2 approaches listed above? Anything I'm doing incorrectly here?
Thank you in advance