[英]Test case to check if Notification Received displays an alert view
I m writing a test case for the following condition 我在写以下情况的测试用例
I Have a class (sub class of NSObject) which makes a service call to add a new customer. 我有一个类(NSObject的子类),该类进行服务调用以添加新客户。 when it gets a successful response it sends a notification to the view co
收到成功的响应后,它会向视图公司发送通知
Now i want to test that the view controller successfully receives notification and displays the correct alert view. 现在,我想测试视图控制器是否成功接收到通知并显示正确的警报视图。
here is my test case code 这是我的测试用例代码
-(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];
}
But this code isn't working. 但是此代码无法正常工作。 It crashes at post notification call.
在通知通知呼叫时崩溃。 Where mi going wrong ?
我哪里错了?
add 加
[[[mockAlertView stub] andReturn:mockAlertView] alloc];
after creating mock object for your alert view. 为警报视图创建模拟对象后。
Is testAlertViewDisplayOnSuccessfullAdditionOfCustomer getting called? 是否正在调用testAlertViewDisplayOnSuccessfullAdditionOfCustomer? You can check by using a breakpoint.
您可以使用断点进行检查。
Perhaps you can use didReceiveLocalNotification in your AppDelegate.m and add your code to the delegate instead? 也许您可以在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 : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.