使用计算的返回值进行单元测试展开 segue

Posted

技术标签:

【中文标题】使用计算的返回值进行单元测试展开 segue【英文标题】:Unit Test unwind segue with calculated returned value 【发布时间】:2015-10-09 04:47:01 【问题描述】:

我正在创建一个单元测试来测试从第二个 ViewController 返回的值。程序流程: 1. FirstVC被点击,然后将数字200传递给SecondVC。 2、SecondVC收到号码,加1。 3. SecondVC 被点击“返回”按钮。它将 201 发送回 FirstVC。

第一VC:

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() simultaneoulsy 执行了断言任务。 如何让.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】:

Unwind segue 将在执行后创建一个新的firstVC。你应该在SecondVCprepareForSegue中得到真正的firstVC

【讨论】:

感谢您的回复。我不知道该怎么做。如果您知道,请指导我解决问题。 URL 或关键字都可以。

以上是关于使用计算的返回值进行单元测试展开 segue的主要内容,如果未能解决你的问题,请参考以下文章

复利计算单元测试

在 Segue 中点击 TableView 单元格返回 Nil

如何在 Xcode 中展开 segue

Java单元测试对void方法的测试

Java单元测试对void方法的测试

Java单元测试对void方法的测试