ios – xctest – 如何在按下按钮时测试新视图是否加载

ios – xctest – 如何在按下按钮时测试新视图是否加载,第1张

概述刚开始使用 xcode 5和xctest.如何在按下按钮时测试视图是否加载.我以编程方式添加了在单击rightBarButtonItem时调用的方法 action:@selector(onSettingsButton) 并在onSettingsButton中 -(void) onSettingsButton{ SettingsViewController *svc = [[Settings @H_403_6@ 刚开始使用 xcode 5和xctest.如何在按下按钮时测试视图是否加载.我以编程方式添加了在单击rightbarbuttonItem时调用的方法

action:@selector(onSettingsbutton)

并在onSettingsbutton中

-(voID) onSettingsbutton{    SettingsVIEwController *svc = [[SettingsVIEwController alloc] init];    [self.navigationController pushVIEwController:svc animated:YES];}

如何编写xctest以确保SettingsVIEwController调出Settings视图?谢谢.

解决方法 您需要进行交互测试 – 即检查对象之间交互的测试.在这种情况下,您要测试-pushVIEwController:animated:在导航控制器上使用SettingsVIEwController进行调用.所以我们想把一个模拟对象放到self.navigationController中,我们可以问,“你按预期调用了吗?”

我将为该类假设一个简单的名称:MyVIEw.

我手工完成的方法是Subclass和OverrIDe navigationController.所以在我的测试代码中,我会做这样的事情:

@interface TestableMyVIEw : MyVIEw@property (nonatomic,strong) ID mockNavigationController;@end@implementation TestableMyVIEw- (UINavigationController *)navigationController{    return mockNavigationController;}@end

现在,测试将创建一个TestableMyVIEw并设置其mockNavigationController属性,而不是创建MyVIEw.

这个mock可以是任何东西,只要它响应-pushVIEwController:animated:并记录参数.这是一个简单的例子,手工:

@interface MockNavigationController : NSObject@property (nonatomic) int pushVIEwControllerCount;@property (nonatomic,strong) UIVIEwController *pushedVIEwController;@property (nonatomic) BOol waspushVIEwControllerAnimated;@end@implementation MockNavigationController- (voID)pushVIEwController:(UIVIEwController *)vIEwController animated:(BOol)animated{    self.pushVIEwControllerCount += 1;    self.pushedVIEwController = vIEwController;    self.waspushVIEwControllerAnimated = animated;}@end

最后,这是一个测试:

- (voID)testOnSettingsbutton_ShouldPushSettingsVIEwController{    // given    MockNavigationController *mockNav = [[MockNavigationController alloc] init];    TestableMyVIEw *sut = [[TestableMyVIEw alloc] init];    sut.mockNavigationController = mockNav;    // when    [sut onSettingsbutton];    // then    XCTAssertEquals(1,mockNav.pushVIEwControllerCount);    XCTAssertTrue([mockNav.pushedVIEwController isKindOfClass:[SettingsVIEwController class]]);}

通过使用模拟对象框架(如Ocmock,Ocmockito或Kiwi的模拟)可以简化这些事情.但我认为首先手动启动是有帮助的,这样你才能理解这些概念.然后选择有用的工具.如果你知道如何手工完成,你永远不会说,“模拟框架X没有做我需要的东西!我被卡住了!”

总结

以上是内存溢出为你收集整理的ios – xctest – 如何在按下按钮时测试新视图是否加载全部内容,希望文章能够帮你解决ios – xctest – 如何在按下按钮时测试新视图是否加载所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1069436.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存