序
前面使用Specta單元測試檢測對象是否泄漏介紹了如何檢測一個普通的NSObject對象是否泄漏谴忧。UIView和UIViewController也是NSObject的子類袋励,當(dāng)然也能使用使用Specta單元測試檢測對象是否泄漏,但是我們知道UIView和UIViewController的應(yīng)用場景比起普通的NSObject對應(yīng)要更加復(fù)雜一些末购。
下面會介紹使用Specta單元測試檢測View和ViewController是否泄漏。
TestContainer 代碼
和之前的文章一樣虎谢,也需要一個TestContainer來weak持有要檢測的對象盟榴。
@interface TestContainer : NSObject
@property (nonatomic, weak) id object;
@end
@implementation TestContainer
// Empty
@end
UIView的檢測
對于UIView的檢測,我們可以創(chuàng)建一個UIWindow婴噩,并且把UIView添加到這個UIWindow里面擎场,然后讓這個UIWindow進行展示,最后再將這個UIWindow設(shè)置成nil几莽,這樣就能模擬這個UIView一個完整的生命周期迅办。
這樣就能檢測UIView是否存在泄漏。
UIView - Spec測試代碼
下面寫檢測代碼章蚣,我們定義一個局部變量weakView站欺, 然后在一個@autoreleasepool里面創(chuàng)建對象, 并且這個weakView在block內(nèi)部置為nil
describe(@"TestObject", ^{
context(@"when created", ^{
it(@"should dealloc", ^{
TestContainer *tc = [TestContainer new];
__weak TestView *weakView = nil;
@autoreleasepool {
TestView *view = [[TestView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
expect(view).beKindOf([UIView class]);
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[window makeKeyAndVisible];
[window addSubview:view];
weakView = view;
expect(weakView).notTo.beNil();
tc.object = weakView;
view = nil;
window = nil;
}
expect(tc.object).after(5).to.beNil();
});
});
});
這里我們斷言:斷定tc的object指針在5秒鐘后究驴,是nil镊绪。
UIView - 模板代碼
sharedExamplesFor(@"view_dealloc_behavior", ^(NSDictionary *data) {
context(@"removed", ^{
it(@"should dealloc", ^{
id (^block)(void) = [[data allValues] firstObject];
if (!block) {
return;
}
TestContainer *tc = [TestContainer new];
__weak UIView *weakView = nil;
@autoreleasepool {
UIView *view = block();
expect(view).beKindOf([UIView class]);
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[window makeKeyAndVisible];
[window addSubview:view];
weakView = view;
expect(weakView).notTo.beNil();
tc.object = weakView;
view = nil;
window = nil;
}
expect(tc.object).after(5).to.beNil();
});
});
});
使用上面的模板代碼匀伏,
describe(@"TestView", ^{
itShouldBehaveLike(@"view_dealloc_behavior", @{ @"value" : ^{
return [[TestView alloc] init];
} });
});
檢測結(jié)果
在Xcode中洒忧,按Command + U
開始測試,上面代碼中够颠, 我們運行的結(jié)果是 Test Success
UIViewController的檢測
對于UIViewController的檢測熙侍,我們同樣可以創(chuàng)建一個UIWindow,并且把UIViewController設(shè)置為這個UIWindow的rootViewController履磨,同樣然后讓這個UIWindow進行展示蛉抓,最后再將這個UIWindow設(shè)置成nil,同樣也就能模擬這個UIViewController一個完整的生命周期剃诅。
這樣就能檢測UIViewController是否存在泄漏巷送。
UIViewController - Spec測試代碼
下面寫檢測代碼,我們定義一個局部變量weakView矛辕, 然后在一個@autoreleasepool里面創(chuàng)建對象笑跛, 并且這個weakView在block內(nèi)部置為nil
describe(@"TestObject", ^{
context(@"when created", ^{
it(@"should dealloc", ^{
TestContainer *tc = [TestContainer new];
__weak TestViewController *weakController = nil;
@autoreleasepool {
TestViewController *controller = [[TestViewController alloc] init];
expect(controller).beKindOf([UIViewController class]);
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[window makeKeyAndVisible];
[window setRootViewController:controller];
weakController = controller;
expect(weakController).notTo.beNil();
tc.object = weakController;
controller = nil;
window = nil;
}
expect(tc.object).after(5).to.beNil();
});
});
});
這里我們斷言:斷定tc的object指針在5秒鐘后,是nil聊品。
UIViewController - 模板代碼
sharedExamplesFor(@"viewController_dealloc_behavior", ^(NSDictionary *data) {
context(@"removed", ^{
it(@"should dealloc", ^{
id (^block)(void) = [[data allValues] firstObject];
if (!block) {
return;
}
TestContainer *tc = [TestContainer new];
__weak UIViewController *weakController = nil;
@autoreleasepool {
UIViewController *controller = block();
expect(controller).beKindOf([UIViewController class]);
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[window makeKeyAndVisible];
[window setRootViewController:controller];
weakController = controller;
expect(weakController).notTo.beNil();
tc.object = weakController;
controller = nil;
window = nil;
}
expect(tc.object).after(5).to.beNil();
});
});
});
使用上面的模板代碼飞蹂,
describe(@"TestViewController", ^{
itShouldBehaveLike(@"viewController_dealloc_behavior", @{ @"value" : ^{
return [[TestViewController alloc] init];
} });
});
檢測結(jié)果
在Xcode中,按Command + U
開始測試翻屈,上面代碼中陈哑, 我們運行的結(jié)果是 Test Success