gpt4 book ai didi

Swift:为 ViewController 中的私有(private)函数创建单元测试

转载 作者:行者123 更新时间:2023-11-28 13:24:53 25 4
gpt4 key购买 nike

我正在尝试测试我的 View Controller ,但我不确定如何测试其中的私有(private)函数。它说是私有(private)的。显然我可以将其公开,但这似乎违背了目的...

   import Quick
import Nimble
@testable import Recur

class ProfileDetailVCSpec: QuickSpec {
class TestProfileDetailVC: ProfileDetailVC {
var isProfileUpdated = false

override func updateProfile() {
isProfileUpdated = true
}
func pressDone() {
doneButtonPressed() //COMPILER WON'T ALLOW, BECAUSE IT'S PRIVATE
}
}
override func spec() {
var testProfileDetailVC: TestProfileDetailVC!
beforeEach {
testProfileDetailVC = TestProfileDetailVC()
}
describe("edit profile") {
context("user makes changes to name") {
it("should call updateProfile") {
testProfileDetailVC.nameTextFieldValidInputEntered(ProfileEditNameView(), "TestFirst", "TestLast")
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(true))
}
}
context("user makes changes to photo") {
it("should call updateProfile") {
testProfileDetailVC.nameTextFieldValidInputEntered(ProfileEditNameView(), "TestFirst", "TestLast")
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(true))
}
}
context("user doesn't make any changes") {
it("should not call updateProfile") {
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(false))
}
}
}
}
}

这是 View Controller 。我的同事仍在研究一些逻辑,但大部分都在那里。我似乎无法快速调用私有(private)函数,所以我无法运行这些测试

class ProfileDetailVC: UIViewController {
private let doneButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Done", for: .normal)
button.titleLabel?.font = UIFont(name: "SFCompactRounded-Semibold", size: 16)
button.tintColor = .recurBlue
button.addTarget(self, action: #selector(doneButtonPressed), for: .touchUpInside)
return button
}()

let profileNameEditView = ProfileEditNameView()
let errorLabel: UILabel = {
let label = UILabel()
label.textColor = .red
label.font = .regularSubtitle
return label
}()

override func viewDidLoad() {
super.viewDidLoad()
loadProfileImage()
setupUI()
profileNameEditView.delegate = self
}

func updateProfile() {

}

private func loadProfileImage() {
if let profile = Profile.currentProfile {
profileImage.configure(with: profile, imageSize: CGSize(width: 120, height: 120))
}
}

@objc private func doneButtonPressed() {
updateProfile()
}

extension ProfileDetailVC: ProfileEditNameViewDelegate {
func nameTextFieldNonValidInputEntered(_: ProfileEditNameView) {
errorLabel.text = "First and last name required"
}

func nameTextFieldValidInputEntered(_: ProfileEditNameView, _ firstNameText: String, _ lastNameText: String) {
errorLabel.text = ""
}
}

最佳答案

无法访问private func 进行测试。但是,您使用的 @testable 属性将允许您在测试中使用 internal func。因此,您可以删除 private 关键字,并且 func 将默认为 internal,因为该类是 internal

关于Swift:为 ViewController 中的私有(private)函数创建单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549422/

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