0
我試圖模擬一個有兩塊的方法的類(AFHTTPSessionManager
),我怎樣才能手動觸發它們,例如,做兩個測試,一個將使用第一個塊實現和另一個測試將使用第二個塊實現。兩塊完成的OCMock存根方法
[manager GET:path parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
//First block to trigger.
//logic code here
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//Second block to trigger.
//logic code here
}];
目前,它會一直觸發第二個塊。
我嘲笑類是這樣的:
NSError *error = [NSError errorWithDomain:NSLocalizedFailureReasonErrorKey code:-1009 userInfo:nil];
NSDictionary *responseObject = [NSDictionary dictionaryWithObjectsAndKeys:@"object", @"key", nil];
id mockAFHTTPSessionManager = OCMClassMock([AFHTTPSessionManager class]);
OCMStub([mockAFHTTPSessionManager alloc]).andReturn(mockAFHTTPSessionManager);
OCMStub([mockAFHTTPSessionManager initWithBaseURL:OCMOCK_ANY]).andReturn(mockAFHTTPSessionManager);
OCMStub([mockAFHTTPSessionManager GET:OCMOCK_ANY parameters:OCMOCK_ANY success:([OCMArg invokeBlockWithArgs:OCMOCK_ANY, responseObject, nil]) failure:([OCMArg invokeBlockWithArgs:OCMOCK_ANY, error, nil])]);