iosobjective-cunit-testingkiwi

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?


Solution

  • 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.

    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.
    Note. I didn't added the implementation of getMemUsage(), you can find out how to implement it here.