【问题标题】:How do I write an Application Test that tests that when a button is pressed, a particular view is shown?如何编写应用程序测试来测试按下按钮时是否显示特定视图?
【发布时间】:2011-08-22 14:45:25
【问题描述】:

我正在查看苹果的example code 进行应用程序测试,这都涉及特定视图内的更改。我想编写一个断言以下内容的测试:

Given that I am seeing the Results view,
And I press the button for the first result,
Then I should see a Detail view,
And its controller's result property should be set to the first result

我将如何编写该测试用例?

【问题讨论】:

    标签: iphone ios unit-testing xcode4


    【解决方案1】:

    您还可以查看 Square 的好人发布的开源集成测试框架。看起来它可以帮助您自动化一些您似乎感兴趣的 UI 级交互测试。

    我自己没有使用过这个框架,但一直想检查一下。

    http://corner.squareup.com/2011/07/ios-integration-testing.html

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在伪代码中,我认为你的测试应该是这样的:

      // in setUp, grab a reference to your desired view controllers & buttons
      // in the test method:
      //   press a tab (or whatever it takes) to navigate to the Results view
      //   press the button for the first result (using target/action or however you have it set up)
           STAssertTrue([myNavigationController.visibleViewController isKindOfClass:[DetailViewController class]]);
           STAssertTrue([(DetailViewController *)myNavigationController.visibleViewController result] == /*first result from earlier*/);
      // in tearDown, do cleanup if necessary
      

      如果这还不够,您应该发布一些代码,以便我们更轻松地提供帮助。


      编辑:关于setUp 的更多信息。我认为值得从您链接到的示例代码中查看实现:

      - (void) setUp {
         app_delegate         = [[UIApplication sharedApplication] delegate];
         calc_view_controller = app_delegate.calcViewController;
         calc_view            = calc_view_controller.view;
      }
      

      app_delegatecalc_view_controllercalc_viewCalcApplicationTests 类的实例变量,因此可以在任何-test... 方法中使用它们。应用程序委托本质上是应用程序的控制器,它具有对主视图控制器的引用。我认为这是一种非常简单和理智的做法。

      【讨论】:

      • 我还没有要发布的代码 - 但这很有帮助,谢谢!
      • 您能否补充一些关于我如何获取对控制器的引用的详细信息?
      • 我也没有这方面的丰富经验,但我会编辑更多关于这方面的信息。
      • 谢谢!事实证明,KIF 是做你描述的事情的好方法......遗憾的是我不能将赏金分成两种方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      相关资源
      最近更新 更多