Overview:
There is a function f1
that returns NSData
.
Is there a way I can write a test case to test a function that returns NSData
?
How do I create the variable expectedOutput
(refer code below) ?
Example:
@interface Car : NSObject
- (NSData*) f1;
@end
@implementation Car
- (NSData*) f1 {
NSData *someData = [[NSData alloc] init]; //This is just an example, the real code has some logic to build the NSData
return someData;
}
@end
Test Case:
- (void) test {
Car *c1;
NSData *actualOutput = [c1 f1];
NSData *expectedOutput = ??? //How can I build this NSData ?
XCTAssertEqualObjects(actualOutput, expectedOutput);
}
Steps
expectedOutput
(NSData
) to file (one time) expectedOutput
expectedOutput
to actualOutput
Step 1:
- (void) writeExpectedDataToFile {}
//Write expected data to file (to be done only once)
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"expected.txt"]];
[data writeToFile:databasePath atomically:YES];
}
Step 2:
- (void) test {
Car *c1;
NSData *actualOutput = [c1 f1];
//Reading from file
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSURL* fileName1 = [bundle URLForResource:@"expected" withExtension:@"txt"];
NSData *expectedData = [NSData dataWithContentsOfFile:[fileName1.path stringByExpandingTildeInPath]];
XCTAssertEqualObjects(actualOutput, expectedOutput);
}