gpt4 book ai didi

ios - 如何验证 kiwi bdd 测试用例中的 didReceiveMemoryWarning 函数?

转载 作者:行者123 更新时间:2023-11-29 01:26:35 25 4
gpt4 key购买 nike

我在使用 kiwi 测试用例验证是否收到内存警告功能时遇到了问题。如何验证功能?

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

有人知道 kiwi 测试用例吗?

最佳答案

您可以直接调用该方法:

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

使用这种方法,您需要遍历 Controller 的属性,您希望这些属性在出现内存警告时被nil-ed

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

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 .

关于ios - 如何验证 kiwi bdd 测试用例中的 didReceiveMemoryWarning 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com