ios - #ifdef 语句检查测试方案

标签 ios objective-c c-preprocessor

我已经为我的项目添加了一个测试目标,现在我想分离不会在应用程序目标上执行的代码部分。假设名为 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];
}

最佳答案

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

TESTING=1

然后就可以写了

#ifdef TESTING
...
#endif

#if TESTING
...
#endif

关于ios - #ifdef 语句检查测试方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843525/

相关文章:

ios - 尝试显示身份验证选择器时 FirebaseUI 崩溃

iphone - 如何使用自定义 NSDataDetector 来检测某些正则表达式模式?

c++ - 正确使用cpp中的定义宏替换函数的名称

ios - 在 block 函数内创建计时器时如何触发计时器?

ios - 我如何在 alamofire post 请求中发送 JSON 变量作为参数

ios - 使用嵌入式 View Controller 设置 view.accessibilityElements

ios - 按住并释放按钮代码继续运行第一个动画完成 block

ios - 在 UIViewController Storyboard 中访问 UIView

c++ - 在 C++ 中标记或定义一组语句

c - 相当于定义名称中的 hash-hash