用于检查Notification Received是否显示警报视图的测试用例

时间:2016-01-20 09:07:52

标签: ios objective-c unit-testing mocking

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

我有一个类(NSObject的子类),它进行服务调用以添加新客户。 当它获得成功的响应时,它会向view 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];
}

但是这段代码不起作用。它在通知后通话时崩溃。哪里出错?

2 个答案:

答案 0 :(得分:1)

添加

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

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

答案 1 :(得分:0)

是否调用了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