gpt4 book ai didi

ios - OCMock 模拟 UIImagePickerController

转载 作者:搜寻专家 更新时间:2023-10-31 23:02:22 102 4
gpt4 key购买 nike

我正在尝试模拟 UIImagePickerController 以测试来自 ViewController 的方法(用 Swift 编写):

var imagePicker: UIImagePickerController!

...

func choosePhoto() {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}

和测试类(用 Objective-C 编写):

接口(interface):

@property (nonatomic, strong) ViewController *viewController;

实现:

- (void)setUp {
[super setUp];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *navigationController = [storyboard instantiateInitialViewController];
self.viewController = (ViewController *)[navigationController visibleViewController];
self.viewController.view;
}

....

- (void)testPicker {
id mockPicker = [OCMockObject mockForClass:[UIImagePickerController class]];
self.viewController.imagePicker = mockPicker;

[[mockPicker expect] setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[[(id)self.viewController expect] presentViewController:mockPicker animated:YES completion:nil];

[self.viewController choosePhoto];

[mockPicker verify];
}

测试失败,因为:

OCMockObject(UIImagePickerController): expected method was not invoked: setSourceType:0

failed: caught "NSInvalidArgumentException", "-[MyApp.ViewController expect]: unrecognized selector sent to instance 0x....

谁能帮我解决这个问题?

非常感谢。

最佳答案

因此,要使用 OCMock 的 expect 方法,您必须有一个期待方法调用的对象的模拟。在这种情况下,您需要为 self.myViewController 创建一个模拟 - 您正在使用的 ViewController 类没有 expect 方法,因此它会变得困惑。您将 VC 转换为 id 这一事实掩盖了这个问题。

关于ios - OCMock 模拟 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052524/

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