ios - 如何在 iOS 9 中对 segue 进行单元测试

标签 ios swift unit-testing

我已经在 Internet 上搜索了几天,但没有确切的例子符合我的情况。
对于简单类。我已经创建了单元测试。

我想对 segue 和 unwind segue 进行单元测试。

我该怎么做?
ProfileTableViewController 是左手边的 Controller 。
SeeDetailViewController 是右手边的 Controller 。
ProfileTableViewController :

import UIKit

class ProfileTableViewController: UITableViewController {

    var profiles = [Profile]();
    var profileNew : Profile?;

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return profiles.count;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "ProfileTableViewCell";
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ProfileTableViewCell
        let profile = profiles[indexPath.row];
        cell.nameLabel.text = profile.name;
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true);
        let row = indexPath.row;
        print("Row:\(row)");
        print(profiles[row].name , profiles[row].age);
        performSegueWithIdentifier("segueTest", sender: row);
    }

    // Mark: Actions
    @IBAction func backFromOtherController(segue: UIStoryboardSegue) {
        NSLog("I'm back from other controller!")
        print(profileNew?.name);

        //add the new profile
        if(profileNew != nil){
            profiles += [profileNew!];
            //update the tableview
            tableView.reloadData();

        }

    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if(segue.identifier == "segueTest"){
            let svc = segue.destinationViewController as! SeeDetailViewController
            let rowid = sender as! Int;
            svc.NamePassed = profiles[rowid].name;
            svc.AgePassed = profiles[rowid].age;
            svc.DescPassed = profiles[rowid].description;
            svc.SpecPassed = profiles[rowid].specialty;

        }
    }
}

SeeDetailViewController :

import UIKit

public class SeeDetailViewController: UIViewController {

    // Mark: Properties
    @IBOutlet weak var NameLabel: UILabel!
    @IBOutlet weak var AgeLabel: UILabel!
    @IBOutlet weak var SpecialtyLabel: UILabel!
    @IBOutlet weak var descTextView: UITextView!

    var NamePassed : String!;
    var AgePassed : Int!;
    var SpecPassed : String!;
    var DescPassed : String!;

    override public func viewDidLoad() {
        super.viewDidLoad()
        NameLabel.text = NamePassed;
        let myString = String(AgePassed);
        AgeLabel.text = myString;
        SpecialtyLabel.text = SpecPassed;
        descTextView.text = DescPassed;
    }

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

    // Mark: Actions
    @IBOutlet weak var HitCall: UIButton!

    @IBAction func alertControllerAction(sender: AnyObject) {
        if(sender.tag == 0 ){
            print("Touch down!");
            let alertController = UIAlertController(title: "Hello!", message: "My name is \(NamePassed)", preferredStyle: .Alert)

            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action:UIAlertAction!) in
                print("you have pressed the Cancel button");
            }
            alertController.addAction(cancelAction)

            let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
                print("you have pressed OK button");
            }
            alertController.addAction(OKAction)

            self.presentViewController(alertController, animated: true, completion:nil)
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */

}

enter image description here

最佳答案

对 View Controller segue 进行单元测试的一种简单方法(尽管它打破了一点抽象)如下:

  1. 创建一个变量,例如,calledSegue,它的初始值为 nil。
  2. 覆盖目标 View Controller 的 performSegueWithIdentifier: 函数,以便将 calledSegue 设置为被调用的 segue 标识符。确保您还在覆盖的函数中调用了 super.performSegueWithIdentifier: 以保留原始函数的行为。
  3. 创建一个单元测试,检查 calledSegue 在测试条件下是否设置为预期的 segue 标识符。

关于ios - 如何在 iOS 9 中对 segue 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470169/

相关文章:

ios - 如何使用 textContentType 在 React Native 中获取 iOS 的电子邮件自动完成建议?

ios - (iOS) 音乐文件停止时的操作

ios - iOS 5中的Popover变更?

ios - 核心数据 : How to update value of type NSSet

php - 如何使用 PHPUnit 的模拟测试名为 "method()"的方法?

ios - 按下 actionSheet 按钮后的警告消息

ios - 'UICollectionView 必须使用非零布局参数进行初始化'

angular - NGXS:测试调度 Action 不适用于 ofActionDispatched

java - TestNG 如何测试 @PreAuthorize 注释及其由 spring MVC Controller 指定的 spring EL?

即使没有碰撞位掩码,swift skspritenode 也会发生碰撞