2016-10-07 25 views
0
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(iOS)的代码覆盖率和XCTest(模拟)

是否有可能在swift中为我的类使用模拟框架?如何做到这一点?

+0

为什么你想嘲笑这个班? – dasdom

回答

0

您可以通过访问测试控制器的视图属性触发viewDidLoad()电话:

_ = viewController.view 

对于其他方法的调用,我就直接给他们打电话。