[英]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: 我正在使用OCMock v3做单元测试,我想测试一个名为
processInfo:
的非常简单的函数,其实现如下所示:
@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. 如您所见,
processInfo:
函数获取信息文件路径并读取数据,然后调用handleData:(NSData*)
函数。 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"
. 我想验证是否使用包含伪字符串
@"dummy info"
的NSData
参数调用了handleData:(NSData*)
函数,但是它失败了,即使实际实现确实通过读取的NSData
对象调用了handleData:(NSData*)
从文件,该文件确实包含NSString
的@"dummy info"
。
I mean looks like OCMVerify()
just simply can not verify it , is it because the dummyInfoData
is not read from file? 我的意思是看起来像
OCMVerify()
只是无法验证它 ,是因为不从文件读取dummyInfoData
吗?
How can I test the handleData:(NSData*)
is called with a NSData
type argument that contains dummy string @"dummy info"
then? 我如何测试带有
NSData
类型参数(包含伪字符串@"dummy info"
handleData:(NSData*)
的handleData:(NSData*)
呢?
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. NSData旨在封装来自各种来源的多种格式的数据,因此,具有相同行为的两个NSData对象实际上不太可能是相同的。 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.
在这种情况下,测试实例可能会保留NSString的副本,而实现实例可能至少要保留文件句柄,直到使用它为止。
In this case, you probably want to use checkWithBlock:
on OCMArg
and once there you can check the class and content. 在这种情况下,你可能想使用
checkWithBlock:
上OCMArg
,一旦那里你可以检查类和内容。 You should compare strings rather than their NSData representations, so compare dummyInfoStr
and [[NSString alloc] initWithData: infoData, encoding: NSUTF8StringEncoding]
. 您应该比较字符串而不是它们的NSData表示形式,因此要比较
dummyInfoStr
和[[NSString alloc] initWithData: infoData, encoding: NSUTF8StringEncoding]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.