Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift iBaction和iBaction的XCT测试(可选)_Swift_Unit Testing_Optional_Xctest - Fatal编程技术网

Swift iBaction和iBaction的XCT测试(可选)

Swift iBaction和iBaction的XCT测试(可选),swift,unit-testing,optional,xctest,Swift,Unit Testing,Optional,Xctest,由于iBaction是可选的,我想知道在Swift中测试iBaction和iBaction的最佳方法是什么 我目前正在重写一个最初用Swift编写的项目,但这次我想使用XCT单元测试 关于iboutlet和ibaction的测试,我已经遇到了一个小问题。这是我的密码: override func setUp() { super.setUp() storyboard = UIStoryboard(name: "Main", bundle: nil) mainMenuVie

由于iBaction是可选的,我想知道在Swift中测试iBaction和iBaction的最佳方法是什么

我目前正在重写一个最初用Swift编写的项目,但这次我想使用XCT单元测试

关于iboutlet和ibaction的测试,我已经遇到了一个小问题。这是我的密码:

override func setUp() {
    super.setUp()

    storyboard = UIStoryboard(name: "Main", bundle: nil)
    mainMenuViewController = storyboard.instantiateViewControllerWithIdentifier("MainMenuViewController") as! MainMenuViewController

    let dummy = mainMenuViewController.view // force loading subviews and setting outlets

    mainMenuViewController.viewDidLoad()
}

override func tearDown() {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    super.tearDown()
}

func testWhetherTranslationGameModeButtonExists() {
    XCTAssert(mainMenuViewController.translateButton != nil, "translate button should exist at all times");
}

func testWhetherTranslateGameModeButtonTitleIsCorrect() {

    let title: String? = mainMenuViewController.translateButton?.titleLabel?.text

    if let unwrappedTitle = title {
        XCTAssertTrue(unwrappedTitle == "Translate Mode", "title of translate button should be Translate Mode")
    }
    else {
        XCTFail("Value isn't set")
    }
}

func testTranslateGameModeButtonIBAction() {

    if let unwrappedButton = mainMenuViewController.translateButton {
        if let actions: Array<String> = unwrappedButton.actionsForTarget(mainMenuViewController, forControlEvent: UIControlEvents.TouchUpInside) as? Array<String> {

            contains(actions, "")
            XCTAssertTrue(contains(actions, "translateButtonPressed"), "translate button should call method translateButtonPressed")
        }
        else {
            XCTFail("button is set but the IBAction is not set")
        }
    }
    else {
        XCTFail("button isn't set and therefore IBAction can not function")
    }
}
覆盖函数设置(){
super.setUp()
故事板=UIStoryboard(名称:“Main”,捆绑包:nil)
mainMenuViewController=情节提要。将EviewControllerWithiIdentifier(“mainMenuViewController”)实例化为!mainMenuViewController
让dummy=mainMenuViewController.view//强制加载子视图和设置出口
mainMenuViewController.viewDidLoad()
}
重写函数拆卸(){
//将拆卸代码放在这里。该方法在调用类中的每个测试方法后调用。
super.tearDown()
}
func TestWheetTranslationGameModeButtonExists(){
xctasert(mainMenuViewController.translateButton!=nil,“翻译按钮应始终存在”);
}
func测试TranslateGamemo去毛刺是否正确(){
let title:String?=mainMenuViewController.translateButton?.titleLabel?.text
如果让unwrappedTitle=标题{
xctasertrue(unwrappedTitle==“翻译模式”,“翻译按钮的标题应为翻译模式”)
}
否则{
XCTFail(“未设置值”)
}
}
func testtranslategamemo debuttonibaction(){
如果让unwrappedButton=mainMenuViewController.translateButton{
如果让actions:Array=unwrappedButton.actionsForTarget(mainMenuViewController,forControlEvent:UIControlEvents.TouchUpInside)作为数组{
包含(操作“”)
XCTASERTTRUE(包含(操作,“translateButtonPressed”),“TranslateButton应调用方法translateButtonPressed”)
}
否则{
XCTFail(“已设置按钮,但未设置iAction”)
}
}
否则{
XCTFail(“未设置按钮,因此iAction无法运行”)
}
}
如您所见,第一个测试检查translate game mode按钮是否存在。这个很好用

然而,对于标题测试,我需要打开按钮,然后测试标签。这似乎是一次两次测试

说到IBAction,我也有同样的问题。我需要打开可选类型以使用按钮,并有一个XCTAssert用于成功和失败。因此,每次我使用这个按钮,我都需要先打开它,然后才能使用它。这似乎是非常重复的,而且似乎是一次测试中的多个测试


我想知道这是否是正确的方法,或者是否有更好的方法来测试optionals。

如果可能,请转到XCode 7。 使用XCode 7,您可以轻松地进行UI测试:

文件->新建->目标->iOS->测试iOS UI测试包

生成的代码有一些如何使用它的提示。基本上,将光标放在测试方法中,然后点击红色记录按钮

因此,您可以模拟(几乎)任何用户操作。当您为可访问性命名UI元素时,生成的代码更具可读性

您的测试结果更有意义,因为您是在控制器的自然环境(即应用程序内部)中测试控制器


(在XCode 7之前是可能的,但使用XCode 7确实很容易做到)

我不明白为什么您的按钮是可选的。 如果通过拖动在控制器中创建插座,则会得到一个非可选变量

我正在设置控制器测试,如下所示:

我的想法来自:

然后,为了完成覆盖,您需要编写3个测试:

  • 如果设置了插座
  • 如果插座连接到动作
  • 如果行动正在发挥作用
通过这种方式,您可以测试所有内容,而无需伪造水龙头之类的东西

func testOutletExist() {
    XCTAssertNotNil(viewController.outletName)
}

func testActionIsConnected() {
    XCTAssertTrue(viewController.outlet.action == "IBactionName:")
}

func testActionCode() {
    let result = IBactionName()
    XCTAssertSomething(result)
}

设置此storyboard=UIStoryboard(名称:“Main”,bundle:nil)时出错??当我改为让storyboard=UIStoryboard(名称:“Main”,bundle:NSBundle(forClass:self.dynamicType))…它可以工作..你知道为什么会发生这种情况吗?当我使用NSBundle(forClass:self.dynamicType))时,我遇到了同样的错误。还不知道为什么。不,我的意思是当我把bundle设置为nil时出错了?Marco,你到底在哪里找到了出口。行动?我希望如此existed@JulioBailon抱歉,在示例中,我正在测试菜单栏