I'm getting started with KIF and am having trouble testing my asynchronously loaded table views with my current configuration.
I have a homescreen in my app with a button. When that button is pressed, a modal view controller is presented.
- (void)viewDidLoad
{
[super viewDidLoad];
// Setup accessibility
self.theTableView.accessibilityLabel = @"My List";
// Register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsLoadedNotification:) name:kNotificationObjectsLoaded object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsFailedToLoadNotification:) name:kNotificationObjectsFailedToLoad object:nil];
// Start loading new data
[[MyListObjectManager sharedInstance] requestObjects];
}
Now, I have setup a test in KIF which looks like this:
+ (id)scenarioToSelecList
{
KIFTestScenario *scenario = [KIFTestScenario scenarioWithDescription:@"Test that a user can select an item from my list."];
[scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"List"]];
[scenario addStep:[KIFTestStep stepToWaitForNotificationName:kNotificationObjectsLoaded object:nil]];
[scenario addStep:[KIFTestStep stepToTapRowInTableViewWithAccessibilityLabel:@"My List" atIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
}
When I run the test, KIF never sees my objects loaded notification.
In debugging, I replaced the [objectManager requestObjects] call in my viewDidLoad method with an asynchronous call to request the objects three seconds later:
[[MyListObjectManager sharedInstance] performSelector:@selector(requestObjects) withObject:nil afterDelay:3.0];
When I do this, I see the following in the KIF output:
PASS (0.90s): Tap view with accessibility label "Find Books"
PASS (3.02s): Wait for notification "notificationObjectsLoaded"
which leads me to believe the original problem is that the notification I'm waiting for is fired before the first step ever finishes executing.
So, the question then becomes, why would the first step take 0.9 seconds to complete? Is it waiting for modal animation to finish before it returns from the step? In that case, the request to load the objects completes faster than the animation.
How should this be handled with KIF? Or is there a different approach to loading the asynchronous data for my tableview that would be more appropriate?
Your suspicion may be correct, that the notification is firing before you run the step that listens for it, but I notice in your output that the step is succeeding.
In any case, if you're loading items in to your table view, your step to tap on one of them will wait until its timeout for them to load—and you can change that timeout. You may find that your test works just fine if you remove the step to wait for the notification entirely.