gpt4 book ai didi

ios - 如何在 Swift 中对私有(private)函数或内部函数进行单元测试?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:58 26 4
gpt4 key购买 nike

所以我创建了一个自定义的抽象类,它继承自 UIViewController(由 RebloodViewController 继承)类,名为 MainViewController。在这个类中我写了一个可重用的 nib 注册函数

class MainViewController: RebloodViewController {

typealias Cell = RebloodViewController.Constants.Cell

internal func registerNib(_ cellNib: Cell.Nib, target: UICollectionView) {

let nib = UINib(nibName: cellNib.rawValue, bundle: nil)

do {
let identifier = try getCellIdentifierByNib(cellNib)
target.register(nib, forCellWithReuseIdentifier: identifier)
} catch {
fatalError("Cell identifier not found from given nib")
}
}

private func getCellIdentifierByNib(_ nib: Cell.Nib) throws -> String {

var identifier: String? = nil

switch nib {
case .articles:
identifier = Cell.Identifier.articles.rawValue
case .events:
identifier = Cell.Identifier.events.rawValue
}

guard let CellIdentifier = identifier else {
throw MainError.cellIdentifierNotFound
}

return CellIdentifier
}

}

对这些私有(private)函数和内部函数进行单元测试的最佳方法是什么?因为我无法访问测试文件中的功能。

最佳答案

您将无法测试私有(private)函数。但是你可以测试内部的。在您的测试中,您导入框架的位置,例如导入 MyFramework,将其更改为:

@testable import MyFramework

关于ios - 如何在 Swift 中对私有(private)函数或内部函数进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48296564/

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