Ios 在UI测试期间访问/修改Xcode中的AppDelegate
在Xcode 7中创建新的UI测试目标时生成的初步框架如下所示:Ios 在UI测试期间访问/修改Xcode中的AppDelegate,ios,xcode,swift,xcode-ui-testing,Ios,Xcode,Swift,Xcode Ui Testing,在Xcode 7中创建新的UI测试目标时生成的初步框架如下所示: override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. // In UI tests it is usually best to stop immed
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
我有一个AppDelegate
,它使用服务提供商访问各种存储库,比如UserService
查找用户等
在我的单元测试包中,我有一个MockUserService
,它允许为测试场景直接操作底层数据一个解决方案是将其添加到主应用程序包中,让AppDelegate检查启动环境,并在给定一些测试标志/环境变量的情况下分配Mock
实现。这里的缺点是,我必须将我的Mock
实现与应用程序一起发布,所以不可以
我想在上面的setUp()
方法中执行以下操作:
XCUIApplication().delegate.serviceProvider = MockServiceProvider()
但我似乎找不到一种简单的方法在UI测试期间访问应用程序委托,使其进入“可测试”状态
在此阶段,如何访问/修改代理或基础应用程序的任何部分?我也想知道这一点,在正常的单元测试中,应用程序代码可用,但在UI测试中,它似乎无法访问AppDelegates之类的内容。尽管这不是您想要的解决方案,但您不能为应用程序创建第二个目标吗,仅在该目标中包含模拟,然后在应用程序委托中定义模拟并测试该目标?那么你就不必运送模拟机了。当然不是很干净的解决方案。@Brynjar这是一种解决方案,是的。缺点(除了明显的缺点)是测试目标驱动整个测试套件。如果对于一个特定的单元测试,我只想模拟一个服务呢?对于另一个测试,我想模拟三个服务?这将需要为每个模拟“排列”设置一个新的目标,而理想情况下,我能够从测试本身驱动模拟性。