Say I have a private method -(void) doSomething
defined in m file of School
class (it is not declared in interface header):
@implementation School
-(void) doSomething {
...
}
@end
I have a unit test case for School
class:
@interface SchoolTest : XCTestCase
@end
@implementation SchoolTest
- (void)testExample {
id mySchoolMock = [OCMockObject partialMockForObject:[School new]];
// I know I can't access '-(void)doSomething' since it is a private method
}
@end
I know I normally can't access this private method. But if I re-declare the -(void) doSomething
in my test class as below:
@interface SchoolTest : XCTestCase
// re-declare it in my test class
-(void) doSomething
@end
@implementation SchoolTest
- (void)testExample {
id mySchoolMock = [OCMockObject partialMockForObject:[School new]];
// Now I can access '-(void)doSomething'!!! Why now I can access the private method in `mySchool` Instance in my test class ?
[mySchoolMock doSomething];
}
@end
Why in above way I can access private method of School
class with mySchool
instance? What is the objective-c theory behind this?
(I am doing this because I have read the answer from this question, but I don't understand why we can do it? what is the theory behind?)