gpt4 book ai didi

ios - 如何使用 OCMock 测试 UIAlertAction 处理程序的内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:06 25 4
gpt4 key购买 nike

我有一个应用程序,我在其中推送一个带有多个自定义 UIAlertActionUIAlertController。每个 UIAlertActionactionWithTitle:style:handler: 的处理程序 block 中执行独特的任务。

我有几个方法需要验证是否在这些 block 中执行。

如何执行 handler block ,以便验证这些方法是否已执行?

最佳答案

经过一番尝试,我终于弄明白了。事实证明,handler block 可以转换为函数指针,并且可以执行函数指针。

像这样

UIAlertAction *action = myAlertController.actions[0];
void (^someBlock)(id obj) = [action valueForKey:@"handler"];
someBlock(action);

这是一个如何使用它的例子。

-(void)test_verifyThatIfUserSelectsTheFirstActionOfMyAlertControllerSomeMethodIsCalled {

//Setup expectations
[[_partialMockViewController expect] someMethod];

//When the UIAlertController is presented automatically simulate a "tap" of the first button
[[_partialMockViewController stub] presentViewController:[OCMArg checkWithBlock:^BOOL(id obj) {

XCTAssert([obj isKindOfClass:[UIAlertController class]]);

UIAlertController *alert = (UIAlertController*)obj;

//Get the first button
UIAlertAction *action = alert.actions[0];

//Cast the pointer of the handle block into a form that we can execute
void (^someBlock)(id obj) = [action valueForKey:@"handler"];

//Execute the code of the join button
someBlock(action);
}]
animated:YES
completion:nil];

//Execute the method that displays the UIAlertController
[_viewControllerUnderTest methodThatDisplaysAlertController];

//Verify that |someMethod| was executed
[_partialMockViewController verify];
}

关于ios - 如何使用 OCMock 测试 UIAlertAction 处理程序的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36926827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com