I want to create unit test that check one particular case. Result of this case is logging with system function NSLog
.
<2014-02-19 03:05:11> Warning bla-bla-bla. Please, check you code.
I've searched a lot, but have not found a decent solution. Is there any chance that logging operation can be captured through observation or mb Xcode console content can be retrieved in some way?
Any advice will be helpful!
EDIT Alright, I see there's some misunderstanding what I want to test. The library want to warn developer, who uses it, about some bad input. It will continue to process them anyway and will return result(mb incorrect result). But at some point of processing I check value(not initial value though) and log warning message and, then, continue to process. Even if I encapsulate this logic I don't want to have this functionality public.
I have the same case , so what you can do is 1.Direct your log to a txt file.
+ (void)recordLog{
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
pathForLog = [documentsDirectory stringByAppendingPathComponent:@"FWTestLog.txt"];
NSLog(@"path = %@",pathForLog);
stderrSave = dup(STDERR_FILENO);
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
2.Search a the string "bla-bla-bla" pattern in the txt file.And use XCTTest to test if the string is found.
3.Close the file and delete it so the other nslog will go back to console.
+ (void)closeLog{
fflush(stderr);
dup2(stderrSave,STDERR_FILENO);
close(stderrSave);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
[fileManager removeItemAtPath:pathForLog error:&error];
}
I wrap it inside my custom test class so that I can use it with other standard XCTTest