【问题标题】:How to test UI Interaction and manipulation of UINavigationController如何测试 UI 交互和 UINavigationController 的操作
【发布时间】:2014-01-20 17:39:22
【问题描述】:
我是 BDD 的新手,我想弄清楚如何编写一个测试来按下我的应用程序中的后退按钮,或者 BDD 是否需要我编写一个测试。
以下是一些具有功能的示例场景:
What: tapBackButton
Scenario: formNotSaved
Result: showAlertNotifiyingTheUser
What: tapBackButton
Scenario: formIsSaved
Result: goes1ScreenBack
我不知道如何为此编写单元测试!我已经添加了 OCMock 框架,但似乎不允许您更改 uiviewcontroller 的导航控制器,因为它是只读的。
我真的很想改变我的开发过程,先写一个失败的测试,然后再写代码,但这很困难。
感谢您的宝贵时间!
【问题讨论】:
标签:
ios
unit-testing
uiviewcontroller
tdd
bdd
【解决方案1】:
我不了解 Apple 的开发,但我可以评论标准 TDD 的东西,这应该会让你朝着正确的方向前进 - 我希望 :)
为此,我要做的是将您的“警报”代码放在一个带有接口的单独类中,例如 IAlerter(除非 Apple 框架已经有一个您可以利用的“消息框/警报”接口)。
将接口注入到你的 UI 类的构造函数中。然后在测试中,你在测试中模拟出 IAlerter。
所以基本上..(在 C# 中,Moq(模拟库)风格,伪代码抱歉;))
//ARRANGE
var alerterMock = new Moq.Mock<IAlerter>();//fake the alerter - cuz you really don't want it to happen, you just want to do a "Verify" that it "was" called.
var ui = new UI(alerterMock.Object);
//ACT
ui.StartDataEntry();
ui.GoBack();
//ASSERT
alerterMock.Verify(a => a.ShowAlert(Moq.It.IsAny<string>()));//verify ShowAlert was called with any string - or at least do a lil ".Contains" to make sure it was calling code that shows the text you expect.
HTH :)