使用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

你可能感兴趣的:(使用Specta单元测试检测View和ViewController是否泄漏)