ios - 如何对正确显示的 UIView 进行单元测试?

标签 ios swift unit-testing

我有一个代表边框 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/

相关文章:

angularjs - 集成测试时在AngularJS中使用$injector(不使用ngMock)

java - 如何使用 JUnit 或 Mockito 测试使用 Random() 的方法(不带参数和返回值)

ios - 我以某种方式搞砸了 Xcode

ios - 我不能在 Swift 中调用多个应用程序消息

Python unittest模拟类和类方法

ios - 如何在 POST 请求中附加数据 - Swift

ios - 移动 Swift 数组

ios - 无法更新 UIScrollView 的 zoomScale

ios - 如何更改 xcode 中的包标识符

ios - 在 UIView 后面捕获(动画)屏幕