ios - 检查收到的通知是否显示警报 View 的测试用例

标签 ios objective-c unit-testing mocking

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

我有一个类(NSObject 的子类),它发出服务调用来添加新客户。 当它获得成功响应时,它会向 View 公司发送通知

现在我想测试 View Controller 是否成功接收通知并显示正确的警报 View 。

这是我的测试用例代码

-(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];
}

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

最佳答案

添加

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

为警报 View 创建模拟对象后。

关于ios - 检查收到的通知是否显示警报 View 的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34895695/

相关文章:

ios - 如何从模态 XIB 设置 VC 的背景颜色

ios - 如何使用xcode为iPad创建新的cocos3D项目?

ios - 如何将核心数据用于主要应用程序和单元测试?

c# - 在单元测试项目中获取具有属性的程序集

ios - 从开发人员门户获取团队列表 - 超时

android - flutter 如何将Future <bool>设置为普通 bool 类型

ios - 都是同步的,但是为什么 "ignoring”函数的NotificationQueue可以重复通知呢?

iphone - 文本多行时如何获取标签的高度

objective-c - UIImagePicker 卡住

android - 如何将 gradle 配置为仅将 logback-classic 用于 Android 中的单元测试?