大佬教程收集整理的这篇文章主要介绍了ios – AFNetworking 2.0和单元测试,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑以使测试失败并在块中设置日志变量
- (void)setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. [[Lsnocilla sharedInstance] start]; stubrequest(@"POST",@"http://www.example.com/module/api/ping"). andReturn(200). withHeaders(@{@"Content-Type": @"application/json"}). withBody(@"{\"success\":truE}"); stubrequest(@"GET",@"http://www.example.com/module/api/ping?testkey=testval"). andReturn(200). withHeaders(@{@"Content-Type": @"application/json"}). withBody(@"{\"success\":truE}"); } - (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; [[Lsnocilla sharedInstance] stop]; [[Lsnocilla sharedInstance] clearstubs]; } - (void)testSanity { AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init]; //[policy setAllowInvalidCertificates:YES]; AFhttprequestOperationManager *manager = [[AFhttprequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.example.com/module/api/ping"]]; //manager.operationQueue = [NSOperationQueue mainQueue]; [manager setSecurityPolicy:policy]; manager.requestserializer = [AFJSONrequestserializer serializer]; manager.responseserializer = [AFJSONResponseserializer serializer]; __block id resObj = nil; __block id resError = nil; AFhttprequestOperation *req = [manager POST:@"http://www.example.com/module/api/ping" parameters:[NSDictionary DictionaryWithObject:@"testval" forKey:@"testkey"] success:^(AFhttprequestOperation *operation,id responSEObject) { NSLog(@"Response: %@",responSEObject); resObj = responSEObject; return; } failure:^(AFhttprequestOperation *operation,NSError *error) { NSLog(@"Error: %@",error); resError = error; return; }]; [req waitUntilFinished]; NSLog(@"req.status: %d",req.response.statusCodE); NSLog(@"req.responSEObj: %@",req.responSEObject); XCTAssertTrue(req.isFinished); NSLog(@"resObj: %@",resObj); NSLog(@"resError: %@",resError); XCTAssertEqual([[req.responSEObject objectForKey:@"success"] boolValue],YES); XCTAssertEqual([[resObj objectForKey:@"success"] boolValue],YES); }
控制台输出
Test Case '-[AppSupportTests testSanity]' started. 2014-04-29 16:45:07.424 xctest[72183:303] req.status: 200 2014-04-29 16:45:07.424 xctest[72183:303] req.responSEObj: { success = 1; } 2014-04-29 16:45:07.424 xctest[72183:303] resObj: (null) 2014-04-29 16:45:07.425 xctest[72183:303] resError: (null) /Users/jlujan/Code/AppSupport/AppSupportTests/AppSupportTests.m:114: error: -[AppSupportTests testSanity] : (([[resObj objectForKey:@"success"] boolValue]) equal to (__objc_yes)) Failed: ("NO") is not equal to ("YES") Test Case '-[AppSupportTests testSanity]' Failed (0.003 seconds).
waitUntilFinished
is once the background operation is complete,并且它不会等到完成块被调用之后.
有一个更好的异步测试框架 – Expecta.@H_616_36@然后而不是调用:
XCTAssertTrue(req.isFinished); XCTAssertEqual([[resObj objectForKey:@"success"] boolValue],YES);
你可以做:
expect(req.isFinished).will.beTruthy(); expect([[resObj objectForKey:@"success"] boolValue]).will.beTruthy();
有lots of other matchers,只需确保在setUp方法中将timeout设置为+[Expecta setAsynchronousTestTimeout:]
.
以上是大佬教程为你收集整理的ios – AFNetworking 2.0和单元测试全部内容,希望文章能够帮你解决ios – AFNetworking 2.0和单元测试所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。