I am very grateful for the tips on testing errors with OCMock.
Test method
It captures the process using an instance of the AppScriptController class.
@implementation ProcessGrabber
-(void)grabProcess {
NSError *error = nil;
NSString *processName = [appScriptController processName:ProcessRef
error:&error];
if(error == nil) {
NSNumber *processID = [appScriptController processID:ProcessRef
error:&error];
if(error == nil) {
... More operations...
}
}
if(error) {
[NSApp raiseError:error];
}
}
@end
Method to extract
The AppScriptController class interacts with the system, so I want to mock it.
@implementation AppScriptController
-(NSString *)processName:(SERef *)theProcessRef error:(NSError **)theError {
return [[theProcessRef name] error:error];
}
-(NSNumber *)processID:(SERef *)theProcessRef error:(NSError **)theError {
return [[theProcessRef name] error:error];
}
Test
-(void)testGrabProcess {
NSError *error = nil;
OCMockObject *mock = [OCMockObject mockForClass:[AppScriptController class]];
[[[mock expect] andReturn:@"Process Name"] processName:nil error:&error];
[[[mock expect] andReturn:34] processID:nil error:&error];
}
Problem
I want to check if the code for handling nested errors works correctly. Therefore, I want to be able to enter a specific error code at a specific point in the method.
<strong> Examples
Simulate an error while capturing a process name. I would check that the capture of the process id is not called.
Simulate an error only when capturing a process identifier. I would verify that the process name is being captured, but no operations are performed after the process ID.
, , .
, , - , "... - ...". , , .
Google , - , , .
, .
- ?