在不同目标之间共享全局常量

时间:2017-10-05 08:13:19

标签: ios swift xcode constants

我在项目中有一些全局常量:

// DemoACIs.swift
let DEMO_TEST_MENU_SCREEN_VIEW_ACI = "test_menu_screen_view"
let DEMO_TEST_MENU_SCREEN_TITLE_LABEL_ACI = "test_menu_screen_title_label"
let DEMO_TEST_MENU_SCREEN_BUTTON1_ACI = "test_menu_screen_button1"
etc.

我想在Xcode中另一个基于Swift的目标(在这种情况下是一个UI测试目标)中引用它们......

// TestMenuScreenViewProxy.swift

import Foundation
import XCTest

class TestMenuScreenViewProxy
{
    internal var view:XCUIElement { return app.otherElements[DEMO_TEST_MENU_SCREEN_VIEW_ACI] }
    internal var titleLabel:XCUIElement { return app.textFields[DEMO_TEST_MENU_SCREEN_TITLE_LABEL_ACI] }
    internal var button1:XCUIElement { return app.buttons[DEMO_TEST_MENU_SCREEN_BUTTON1_ACI] }
}

当然这不起作用。在测试目标中找不到常量。如何在目标中访问它们?

1 个答案:

答案 0 :(得分:4)

选择已声明常量的文件并更新其目标成员资格,使其可用于测试目标

enter image description here

以下是示例代码

import Foundation

class Constants {
    static let test = "ABCD"
}

更改了目标成员资格,使其在测试目标中可用

现在处于测试目标

override func setUp() {
    super.setUp()
    let c  = Constants.test
    // Put setup code here. This method is called before the invocation of each test method in the class.
}