简体   繁体   English

如何验证猕猴桃bdd测试用例中的didReceiveMemoryWarning功能?

[英]How to verify didReceiveMemoryWarning function in kiwi bdd test cases?

I'm facing issue to verify did receive memory warning function in by using kiwi test cases. 我面临的问题是要验证是否使用猕猴桃测试用例接收了内存警告功能。 how to verify the function? 如何验证功能?

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

Any one know about kiwi test case? 有人知道猕猴桃测试用例吗?

You can simply call the method directly: 您可以直接直接调用该方法:

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

With this approach you'll need to walk through the properties of the controller that you expect to be nil-ed in the eventuality of a memory warning. 通过这种方法,你需要通过你希望是控制器的性能走路nil-ed在内存警告的可能性。

Or, you can check the memory usage of the unit test app, and see if the memory got reduced after the simulated memory warning. 或者,您可以检查单元测试应用程序的内存使用情况,并在模拟内存警告后查看内存是否减少了。 This is not as accurate as the first approach, but it can give you some hints. 这不如第一种方法准确,但可以给您一些提示。 And you also will need to make sure that the controller will be rendered on screen, or at least to make it think it's rendered and start building views/ table view cells, etc. 而且,您还需要确保控制器将在屏幕上呈现,或者至少使其认为已呈现,然后开始构建视图/表格视图单元等。

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
});

You need to use an autorelease pool to have control of the the objects that are created with autorelease , as those objects will get released when your autorelease pool scope ends, and not when the main autorelease pool gets drained. 您需要使用一个autorelease pool具有了与创建对象的控制autorelease ,因为这些物体会当你得到释放autorelease pool范围结束时的主,而不是autorelease pool被耗尽。
Note. 注意。 I didn't added the implementation of getMemUsage() , you can find out how to implement it here . 我没有添加getMemUsage()的实现,您可以在此处了解如何实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM