如何在kiwi bdd测试用例中验证didReceiveMemoryWarning函数?

时间:2015-11-26 09:12:00

标签: ios objective-c unit-testing kiwi

我正面临通过使用 kiwi 测试用例验证确实已收到内存警告功能的问题。如何验证功能?

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

有人知道奇异果测试用例吗?

1 个答案:

答案 0 :(得分:1)

您可以直接调用该方法:

it(@"Should cleanup when receiving a memory warning", ^{
    [controller didReceiveMemoryWarning];
    // assert here that the data that you expected was released
});

使用这种方法,您需要在内存警告发生的情况下浏览控制器的属性nil-ed

或者,您可以检查单元测试应用程序的内存使用情况,并查看模拟内存警告后内存是否减少。这不如第一种方法准确,但它可以给你一些提示。而且你还需要确保控制器将在屏幕上呈现,或者至少让它认为它被渲染并开始构建视图/表视图单元格等。

it(@"Should cleanup when receiving a memory warning", ^{
    vm_size_t memoryBeforeMemWarning;
    vm_size_t memoryAfterMemWarning;
    MyController *controller = nil;

    @autoreleasepool {
        controller = ...;
        // call controller.view, or other methods that create the view
        // also call any other methods that trigger subview creation

        memoryBeforeMemWarning = getMemUsage();
        //simulate the memory warning
        [controller didReceiveMemoryWarning];
    }
    memoryAfterMemWarning = getMemUsage();

    // reference the variable here to make sure ARC doesn't
    // release it when it detects its last reference
    controller = nil;

    // now assert upon the difference between the two reported memory usages
});

您需要使用autorelease pool来控制使用autorelease创建的对象,因为这些对象将在autorelease pool范围结束时释放,而不是主autorelease pool被耗尽。
注意。我没有添加getMemUsage()的实施,您可以了解如何实施here