我有一个代表边框 View 的 UIView
(contentBGView
)。任务是如果 url 可用则显示边框,如果不可用则显示无边框。
如何对这段代码进行单元测试?
if contentUrlExists {
addSubview(contentBGView)
contentBGView.addSubview(contentTextView)
contentBGView.addSubview(mediaView)
contentBGView.isHidden = false
statesTextView.anchor(locationLabel.bottomAnchor, left: self.leftAnchor, bottom: contentBGView.topAnchor, right: self.rightAnchor, topConstant: 5, leftConstant: 10, bottomConstant: 2, rightConstant: 5, widthConstant: 0, heightConstant: 0)
contentBGView.anchor(statesTextView.bottomAnchor, left: self.leftAnchor, bottom: statisticsSlidingBarCollectionView.topAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 5, bottomConstant: 10, rightConstant: 5, widthConstant: 0, heightConstant: 0)
mediaView.anchor(contentBGView.topAnchor, left: contentBGView.leftAnchor, bottom: nil, right: contentBGView.rightAnchor, topConstant: 5, leftConstant: 4, bottomConstant: 2, rightConstant: 4, widthConstant: 0, heightConstant: 0)
contentTextView.anchor(mediaView.bottomAnchor, left: contentBGView.leftAnchor, bottom: contentBGView.bottomAnchor, right: contentBGView.rightAnchor, topConstant: 5, leftConstant: 5, bottomConstant: 2, rightConstant: 5, widthConstant: 0, heightConstant: stateConetentTextHeight)
statisticsSlidingBarCollectionView.anchor(contentBGView.bottomAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 5, bottomConstant: 0, rightConstant: 5, widthConstant: 0, heightConstant: 45)
}else{
addSubview(mediaView)
contentBGView.isHidden = true
statesTextView.anchor(locationLabel.bottomAnchor, left: self.leftAnchor, bottom: mediaView.topAnchor, right: self.rightAnchor, topConstant: 5, leftConstant: 10, bottomConstant: 2, rightConstant: 5, widthConstant: 0, heightConstant: 0)
mediaView.anchor(statesTextView.bottomAnchor, left: self.leftAnchor, bottom: statisticsSlidingBarCollectionView.topAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 2, rightConstant: 0, widthConstant: 0, heightConstant: 160)
statisticsSlidingBarCollectionView.anchor(mediaView.bottomAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 5, bottomConstant: 0, rightConstant: 5, widthConstant: 0, heightConstant: 45)
}
边框的单元测试根据内容 URL 正确显示。
谢谢
最佳答案
这取决于您要测试的部分和程度。如果 contentBGView
对您的 UI 测试可见,那么您可以简单地测试它是否为 isHidden
。如果你想更具体一些,你可以将各种 subview 与预期值进行比较,但在我看来,在那个级别进行测试是没有意义的。
关于ios - 如何对正确显示的 UIView 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174721/