gpt4 book ai didi

ios - 具有计算返回值的单元测试展开 segue

转载 作者:行者123 更新时间:2023-11-28 06:57:27 26 4
gpt4 key购买 nike

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

程序流程:
1. FirstVC被点击,然后将数字200传递给SecondVC。
2. SecondVC 收到数字并加1。
3. SecondVC 被点击“后退”按钮。它向 FirstVC 发回 201。

第一风投:

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);
}
}
}

第二个VC.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 失败:(“可选(200)”)不等于(“可选(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);
}
}

最佳答案

Unwind segue 执行后会创建一个新的firstVC。您应该在 SecondVCprepareForSegue

中获得真正的 firstVC

关于ios - 具有计算返回值的单元测试展开 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com