使用Specta单元测试检测View和ViewController是否泄漏

前面使用Specta单元测试检测对象是否泄漏介绍了如何检测一个普通的NSObject对象是否泄漏。UIView和UIViewController也是NSObject的子类,当然也能使用使用Specta单元测试检测对象是否泄漏,但是我们知道UIView和UIViewController的应用场景比起普通的NSObject对应要更加复杂一些。

下面会介绍使用Specta单元测试检测View和ViewController是否泄漏。

TestContainer 代码

和之前的文章一样,也需要一个TestContainer来weak持有要检测的对象。


@interface TestContainer : NSObject

@property (nonatomic, weak) id object;

@end


@implementation TestContainer
// Empty
@end

UIView的检测

对于UIView的检测,我们可以创建一个UIWindow,并且把UIView添加到这个UIWindow里面,然后让这个UIWindow进行展示,最后再将这个UIWindow设置成nil,这样就能模拟这个UIView一个完整的生命周期。

这样就能检测UIView是否存在泄漏。

UIView - Spec测试代码

下面写检测代码,我们定义一个局部变量weakView, 然后在一个@autoreleasepool里面创建对象, 并且这个weakView在block内部置为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];
    } });
});
检测结果

在Xcode中,按Command + U开始测试,上面代码中, 我们运行的结果是 Test Success

UIViewController的检测

对于UIViewController的检测,我们同样可以创建一个UIWindow,并且把UIViewController设置为这个UIWindow的rootViewController,同样然后让这个UIWindow进行展示,最后再将这个UIWindow设置成nil,同样也就能模拟这个UIViewController一个完整的生命周期。

这样就能检测UIViewController是否存在泄漏。

UIViewController - Spec测试代码

下面写检测代码,我们定义一个局部变量weakView, 然后在一个@autoreleasepool里面创建对象, 并且这个weakView在block内部置为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];
    } });
});
检测结果

在Xcode中,按Command + U开始测试,上面代码中, 我们运行的结果是 Test Success

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,150评论 1 32
  • 重点参考链接: View Programming Guide for iOS https://developer....
    Kevin_Junbaozi阅读 4,554评论 0 15
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 1,396评论 0 3
  • 7、不使用IB是,下面这样做有什么问题? 6、请说说Layer和View的关系,以及你是如何使用它们的。 1.首先...
    AlanGe阅读 726评论 0 1
  • 彼岸花开开彼岸,奈何桥头无奈何(:-*)
    d418f903ca49阅读 220评论 0 1