简体   繁体   中英

XCTest .xib of custom UIView in Swift

How do you XCTest a .xib who's class is set to a custom class of type UIView?

I want to unit test this, and am thinking I need to instantiate the view (complete with IBOutlets) from nib, then test things like: tempSearchCardView.titleLabel.text == "Hello World"

I've tried tempSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: nil, options: nil)![0] as? SearchCardView tempSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: nil, options: nil)![0] as? SearchCardView but tempSearchCardView = nil

...

Here's my IB situation:

在此处输入图片说明

...

Here's my .swift file:

在此处输入图片说明

I found an answer here: Swift 2.0: Could not cast value MyApp.MyCustomClass to MyAppTests.MyCustomClass when using Set

I tried most every other tutorial/advice I could find.

The above post says to remove the files from the test target, and simply @testable import MyApp ... then testSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: self, options: nil)?.first as! SearchCardView testSearchCardView = Bundle.main.loadNibNamed("SearchCardView", owner: self, options: nil)?.first as! SearchCardView worked as expected!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM