bazel 提供了 unit test和ui test的rules
分別是:
ios_unit_test 和 ios_ui_test
但是當(dāng)我們嘗試用ios_unit_test 去寫https網(wǎng)絡(luò)請(qǐng)求的時(shí)候會(huì)出現(xiàn)請(qǐng)求失敗的現(xiàn)象
如圖

image.png
錯(cuò)誤日志:
2021-12-14 15:44:20.491 xctest[11320:31711385] 請(qǐng)求開(kāi)始
2021-12-14 15:44:20.801 xctest[11320:31711424] 請(qǐng)求完成...
2021-12-14 15:44:20.802 xctest[11320:31711424] error------- : The certificate for this server is invalid. You might be connecting to a server that is pretending to be “baidu.com” which could put your confidential information at risk.
Test Case '-[UserTests testDown]' passed (0.325 seconds).
Test Suite 'UserTests' passed at 2021-12-14 15:44:20.805.
Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.326) seconds
Test Suite 'User_test.xctest' passed at 2021-12-14 15:44:20.806.
Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.327) seconds
Test Suite 'All tests' passed at 2021-12-14 15:44:20.806.
Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.350) seconds
BUILD代碼
objc_library(
name = "User_test_library",
testonly = 1,
srcs = glob(["UserTests/*.m"]),
)
ios_unit_test(
name = "User_test",
minimum_os_version = "9.0",
deps = [
":User_test_library",
]
)
UserTests代碼:
#import <XCTest/XCTest.h>
#import <UIKit/UIKit.h>
@interface UserTests : XCTestCase
@end
@implementation UserTests
- (void)setUp {
}
- (void)testDown {
NSLog(@"請(qǐng)求開(kāi)始");
XCTestExpectation *expectation = [self expectationWithDescription:@"異步加載 Person"];
NSString *url = @"https://baidu.com";
NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"請(qǐng)求完成...");
if (!error) {
NSString * str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str-------------:%@",str);
[expectation fulfill];
} else {
NSLog(@"error------- : %@", error.localizedDescription);
[expectation fulfill];
}
}];
[task resume];
[self waitForExpectationsWithTimeout:5 handler:nil];
}
@end
發(fā)現(xiàn)在unit的時(shí)候, 使用https報(bào)The certificate for this server is invalid錯(cuò)誤 ,查閱資料, 都沒(méi)有找到相關(guān)記載,于是翻看rules_apple 和xctestrunner 這兩個(gè)rules是直接和ios_unit_test相關(guān)的兩個(gè)rules
大致流程如下:

image.png