gpt4 book ai didi

ios - Swift (iOS) 代码覆盖率和 XCTest(模拟)

转载 作者:行者123 更新时间:2023-11-30 13:00:51 25 4
gpt4 key购买 nike

  class MyClassVC: UIViewController {

var html : String?
var heightBar: CGFloat?
var webView: UIWebView?

override func viewDidLoad() {
super.viewDidLoad()

heightBar = self.navigationController?.navigationBar.frame.height

webView = UIWebView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.width, height: self.view.frame.height - heightBar!))

let resources = NSBundle.mainBundle().pathForResource("example", ofType: "html")
let css = NSURL(fileURLWithPath: resources!)

webView!.loadHTMLString(html!, baseURL: css)

setNavBar()

self.view.addSubview(webView!)
}

func close(sender: UIBarButtonItem){
dismissViewControllerAnimated(true, completion: nil)
}

func setNavBar(){
var title : NSDictionary = Dictionary<NSObject,AnyObject>();
title = [NSFontAttributeName:UIFont.systemFontOfSize(20), NSForegroundColorAttributeName: UIColor(netHex: AppColors().getAppColors("colors"))]
self.navigationController!.navigationBar.titleTextAttributes = title as? [String : AnyObject]

navigationItem.rightBarButtonItem = UIBarButtonItem( title: "Close", style: .Plain, target: self, action: #selector(MyClassVC.close(_:)))
}
}

我上面有这个类,我需要为这个类创建一个XCTestCase并覆盖所有代码。在 XCTestCase 中,我需要调用 viewDidLoad()、close() 和 setNavBar() 函数,如何正确执行此操作?

是否可以在 swift 中为我的类使用模拟框架?怎么才能做到这一点?

最佳答案

您可以通过在测试中访问 Controller 的view属性来触发viewDidLoad()的调用:

_ = viewController.view

对于其他方法的调用,我会直接调用它们。

关于ios - Swift (iOS) 代码覆盖率和 XCTest(模拟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39919371/

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