【发布时间】:2014-07-22 21:04:42
【问题描述】:
我正在开发一个自定义网络库并为其编写一些单元测试用例。 我不知道该怎么做。
我有一个 RequestObject,它是 NSOperation 的子类
@interface 请求操作:NSOperation @property (nonatomic, strong) NSURLRequest *URLRequest; @property (nonatomic, strong, readonly) NSURLResponse *URLResponse; @property (nonatomic, strong, readonly) NSError *connectionError; @property (nonatomic, strong, readonly) NSData *responseData; -(id)initWithURLRequest:(NSURLRequest*) 请求; @结尾在实现中,我有一个具有 NSURLConnection 的私有类别。
我想写一个测试用例来检查请求发送后 URLResponse 是否存在
-(void)testIfResponseExist { NSURL *url = [[NSURL alloc] initWithString:@"https://google.com"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; myRequest = [[RequestOperation alloc] initWithURLRequest:request]; [myRequest 开始]; [自我等待:1]; XCTAssertNotNil(myRequest.URLResponse, @"Response 不应该是 nil"); }这种单元测试方式正确吗?
现在这是向服务器发送实际请求。但是,我也可以存根 NSURLConnection 并发送虚拟响应。但我不确定我应该走哪条路。 模拟对象的优缺点是什么?
【问题讨论】:
标签: ios unit-testing ocmock xctest