Testing Nested Error Handling with OCMock

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];

  // ... Somehow inject an error here...

  [[[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 , - , , .

, .

- ?

+3
4

, , - . , , OCMock - , ? , NSError ** ( ). , , ?

+3

- , OCMock:

- (void)testGrabProcess {

    OCMockObject *mock = [OCMockObject mockForClass:[AppScriptController class]];
    [[[[mock expect] andDo:^(NSInvocation *invocation) {
        NSError * __autoreleasing *anError = nil;
        [invocation getArgument:&anError atIndex:3];
        *anError = [NSError errorWithDomain:@"MYDomain" code:2323 userInfo:nil];
    }] andReturn:@"Process Name"] processName:nil error:&error];

    [[[mock expect] andReturnValue:@34] processID:nil error:(BECloudPlayerDevice *__autoreleasing *)[OCMArg anyPointer]];
}
+1

nil;

-(void)testGrabProcessError {
  NSError *error = //create the error code you want to test here.;        

  OCMockObject *mock = [OCMockObject mockForClass:[AppScriptController class]];
  [[[mock expect] andReturn:nil] processName:nil error:&error];
  ...
}
0

:

MyClass :

- (BOOL) myMethod:(id)someParameter error:(NSError **)error;

:

id myMock = [OCMockObject mockForClass:[MyClass class]];
[[myMock expect] myMethod:someParameter error:(NSError *__autoreleasing *)[OCMArg anyPointer]];

MyClass.

0

Source: https://habr.com/ru/post/1712392/


All Articles