检查收到的通知是否显示警报视图的测试用例

Test case to check if Notification Received displays an alert view

我正在为以下条件编写测试用例

我有一个 class(NSObject 的子 class),它发出服务调用以添加新客户。 当它获得成功响应时,它会向视图 co

发送通知

现在我想测试视图控制器是否成功接收到通知并显示正确的警报视图。

这是我的测试用例代码

-(void) testAlertViewDisplayOnSuccessfullAdditionOfCustomer{

    id mockAlertView = [OCMockObject mockForClass:[UIAlertView class]];

    (void)[[[mockAlertView expect] andReturn:mockAlertView]
       initWithTitle:@"myAppName"
       message:@"Submitted Successfully"
       delegate:nil
       cancelButtonTitle:@"OK"
       otherButtonTitles:OCMOCK_ANY, nil];



    [[mockAlertView expect] show];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"notifcationName" 
    object:nil userInfo:@{@"mykey" : @"Submitted"}];

    [mockAlertView verify];
}

但是这段代码不起作用。它在 post 通知调用时崩溃。我哪里错了?

是否调用了 testAlertViewDisplayOnSuccessfullAdditionOfCustomer?您可以使用断点进行检查。

也许您可以在 AppDelegate.m 中使用 didReceiveLocalNotification 并将您的代码添加到委托中?

- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:

添加

[[[mockAlertView stub] andReturn:mockAlertView] alloc];

为警报视图创建模拟对象后。