简体   繁体   中英

in unit test, verify function called with argument NSData (with a NSString in it)

I am using OCMock v3 do unit testing, I want to test a very simple function named processInfo: , its implementation is showing below:

@implementation MyService
-(void) processInfo{
  // get info file path
  NSString *infoFilePath = [self getInfoFile];
  // read info data from infoFile
  NSData *infoData = [[NSData alloc] initWithContentsOfFile:infoFilePath];

  // call another function to handle info data
  [self handleData:infoData];
}

-(void) handleData:(NSData*) infoData {
   ...
}

@end

As you see, the processInfo: function gets info file path & read data out then call handleData:(NSData*) function. Pretty simple logic.

I tried to test the above simple function in following way:

-(void) testProcessInfo{
  // create dummy info string
  NSString* dummyInfoStr = @"dummy info";
  // convert above NSString to NSData object
  NSData* dummyInfoData = [dummyInfoStr dataUsingEncoding:NSUTF8StringEncoding];

  // get the same info file path
  NSString* infoFilePath=[self getInfoFile];
  // write dummy info data to info file
  [data writeToFile:path options:NSDataWritingAtomic error:nil];

  // CALL function under test
  [myServicePartialMock processInfo];

  // I want to verify that handleData:(NSData*) has been invoked with a NSData argument which contains dummy string @"dummy info"
  // BUT it failed, even though the real implementation does it.
  // For some reason the dummyInfoData is not considered equal to the NSData used in real implementation, though they both contain string @"dummy info"
  OCMVerify([myServicePartialMock handleData:dummyInfoData]);
}

I want to verify that function handleData:(NSData*) is called with a NSData argument which contains dummy string @"dummy info" , but it failed, even though the real implementation did invoke handleData:(NSData*) with a NSData object read from file which does contain NSString of @"dummy info" .

I mean looks like OCMVerify() just simply can not verify it , is it because the dummyInfoData is not read from file?

How can I test the handleData:(NSData*) is called with a NSData type argument that contains dummy string @"dummy info" then?

NSData is designed to encapsulate data in a large variety of formats from a variety of sources, so two NSData objects which have identical behaviour are not likely to be actually identical. In this case, the test instance is probably retaining a copy of the NSString, and the implementation instance is probably retaining a file handle, at least until it's used.

In this case, you probably want to use checkWithBlock: on OCMArg and once there you can check the class and content. You should compare strings rather than their NSData representations, so compare dummyInfoStr and [[NSString alloc] initWithData: infoData, encoding: NSUTF8StringEncoding] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM