【发布时间】:2015-04-18 11:56:29
【问题描述】:
我有以下要为其创建单元测试的代码。我想检查 self.response 是否不为零
- (void)downloadJson:(NSURL *)url {
// Create a download task.
NSURLSessionDataTask *task = [[NSURLSession sharedSession]
dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response,
NSError *error) {
if (!error) {
NSError *JSONError = nil;
NSDictionary *dictionary =
[NSJSONSerialization JSONObjectWithData:data
options:0
error:&JSONError];
if (JSONError) {
NSLog(@"Serialization error: %@", JSONError.localizedDescription);
} else {
NSLog(@"Response: %@", dictionary);
self.response = dictionary;
}
} else {
NSLog(@"Error: %@", error.localizedDescription);
}
}];
// Start the task.
[task resume];
}
我目前拥有的是
- (void)testDownloadJson {
XCTestExpectation *expectation =
[self expectationWithDescription:@"HTTP request"];
[self.vc
downloadJson:[NSURL URLWithString:@"a json file"]];
[self waitForExpectationsWithTimeout:5
handler:^(NSError *error) {
// handler is called on _either_ success or
// failure
if (error != nil) {
XCTFail(@"timeout error: %@", error);
} else {
XCTAssertNotNil(
self.vc.response,
@"downloadJson failed to get data");
}
}];
}
但这当然是不正确的。想知道是否有人可以帮助我了解如何编写此测试。
谢谢
【问题讨论】: