#ifdef语句用于检查测试方案。

4

我为我的项目添加了一个测试目标,现在我想分离代码的部分,在应用程序目标上不执行。假设应用程序称为TestMyApp,我希望它像这样:

-(void)addDevice:(Account*)account{
NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];    


NSDictionary *params = @{@"device_id":account.deviceID,
                         @"token":account.deviceToken ? account.deviceToken : @"fuygciureygfiyuergfyurgfuyergfuyerguy",
                         @"type":account.deviceName,
                         @"os_type":@"ios",
                         @"os_version":account.os,
                         @"accounts":_accounts};


NSString *js = [jsonWriter stringWithObject:params];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"PUT" path:@"/device" parameters:nil];
NSData *requestData = [NSData dataWithBytes:[js UTF8String] length:[js length]];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        if (error) {
            NSLog(@"%@",JSON);
        }
        NSLog(@"%@",JSON);
        if ([[JSON objectForKey:@"status"]isEqualToString:@"success"]) {

***#ifdef TestMyApp
   some code here***
  [self.testDelegate addDeviceJson:JSON type:@"addDevice"];
                                                            [self deviceAdded:[JSON objectForKey:@"uid"]];
                                                            [self.accountsDelegate deviceDidAdded];
                                                        }
                                                    }
                                                                        failure:^(AFHTTPRequestOperation *operation, NSError *error){
                                                                        }];
[httpClient enqueueHTTPRequestOperation:operation];
}
2个回答

7

前往目标,预处理部分。在“预处理器宏”中,您可以为任何方案添加定义。例如:

TESTING=1

那么你可以写下以下内容:

然后你可以写

#ifdef TESTING
...
#endif

或者
#if TESTING
...
#endif

1
创建两个pch文件,并将它们添加到每个目标中。现在定义一些常量字符串,并根据此处理代码更改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接