单元测试展开segue并计算返回值

时间:2015-10-09 04:47:02

标签: ios swift unit-testing

我正在创建一个单元测试来测试从第二个ViewController返回的值。

计划流程
1.点击FirstVC然后将号码200传递给SecondVC 2. SecondVC接收数字并将其增加1.
3. SecondVC点击了“返回”按钮。它将201发送回FirstVC。

FirstVC:

import UIKit

class FirstVC: UIViewController {

    var number : Int?;
    var destVC : SecondVC?;

    override func viewDidLoad() {
        super.viewDidLoad()
        number = 200;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func hitButton(sender: AnyObject) {
        performSegueWithIdentifier("goNext", sender: self);
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let svc = segue.destinationViewController as! SecondVC;
        svc.receivedNumber = self.number;
        self.destVC = svc
    }
    @IBAction func backToFirstVC(segue: UIStoryboardSegue){
        if(segue.identifier == "backSegue"){
            let srcVC = segue.sourceViewController as! SecondVC;
            self.number = srcVC.receivedNumber;
            print(self.number);
        }
    }
}

SecondVC.swift:

import UIKit

class SecondVC: UIViewController {
var receivedNumber : Int?;
@IBOutlet weak var numberLbl: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.receivedNumber = self.receivedNumber! + 1;
    self.numberLbl.text = String(self.receivedNumber!);
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func hitBackBtn(sender: AnyObject) {
    performSegueWithIdentifier("backSegue", sender: self);
}
}

FirstVCTests.swift:

import XCTest
@testable import practiceTesting

class practiceTestingTests: XCTestCase {

    var firstVC :practiceTesting.FirstVC!
    var storyboard : UIStoryboard!;
    var storyboardsegue : UIStoryboardSegue!;

    override func setUp() {
        super.setUp()
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        firstVC = storyboard.instantiateViewControllerWithIdentifier("FirstVC") as! practiceTesting.FirstVC;
        _ = firstVC.view;
    }

    func testExample() {
        firstVC.hitButton(self);
        firstVC.destVC!.hitBackBtn(self);
        XCTAssertEqual(firstVC.number, 201)
    }
}

错误消息: XCTAssertEqual失败:(“Optional(200)”)不等于(“Optional(201)”)

似乎.hitButton()尚未完成工作,但XCTAssertEqual()同时执行断言任务。

如何让.hitButton()等待secondVC的计算,然后断言返回的值?

更新 我现在将再次回到我的主题。我将测试一个函数。

import XCTest

@testable import practiceTesting

class FirstVCTests: XCTestCase {

    var firstVC :practiceTesting.FirstVC!
    var secondVC:practiceTesting.SecondVC!
    var storyboard : UIStoryboard!;
    var segue1 : UIStoryboardSegue!;
    var segue2 : UIStoryboardSegue!;

    override func setUp() {
        super.setUp()
        storyboard = UIStoryboard(name: "Main", bundle: nil);
        firstVC = storyboard.instantiateViewControllerWithIdentifier("FirstVC") as! practiceTesting.FirstVC;
        secondVC = storyboard.instantiateViewControllerWithIdentifier("SecondVC") as! practiceTesting.SecondVC;
        segue1 = UIStoryboardSegue.init(identifier: "goNext", source: firstVC, destination: secondVC)
        segue2 = UIStoryboardSegue.init(identifier: "backSegue", source: secondVC, destination: firstVC);
        _ = firstVC.view;
    }

    func testB(){
        secondVC.receivedNumber = 300;
        firstVC.backToFirstVC(segue2);                    
        XCTAssertEqual(firstVC.number, 300);
    }
}

1 个答案:

答案 0 :(得分:1)

展开segue将在执行后创建新的firstVC。您应该在firstVC SecondVC

中获得真实的prepareForSegue