Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中测试自定义UITableViewCell的布局_Ios_Uitableview_Swift_Unit Testing_Xctest - Fatal编程技术网

Ios 如何在Swift中测试自定义UITableViewCell的布局

Ios 如何在Swift中测试自定义UITableViewCell的布局,ios,uitableview,swift,unit-testing,xctest,Ios,Uitableview,Swift,Unit Testing,Xctest,我为我创建的自定义单元格创建了一个测试类。单元格相当复杂,有几个不同的标签和图像,这些标签和图像自动布局到单元格的边界,我想测试为某些标签设置长文本不会使标签超过单元格的边界。 基本上,我想运行以下行: XCTAssertLessThanOrEqual(cell.textLabel!.frame.size.width, cell.frame.size.width, "text label width should be less than cell's frame") 以确保我的自定义文本标签

我为我创建的自定义单元格创建了一个测试类。单元格相当复杂,有几个不同的标签和图像,这些标签和图像自动布局到单元格的边界,我想测试为某些标签设置长文本不会使标签超过单元格的边界。 基本上,我想运行以下行:

XCTAssertLessThanOrEqual(cell.textLabel!.frame.size.width, cell.frame.size.width, "text label width should be less than cell's frame")

以确保我的自定义文本标签不会超出边界。不过,我遇到的问题是,我不知道如何模拟上面的自定义单元格,
cell
。我是否需要创建一个模拟表视图并用模拟数据填充该视图,然后获得一个模拟单元格,或者是否有方法创建
UITableViewCell
的自定义子类的实例并立即测试其布局?

测试可能如下所示(参考:)


这不是一个很好的测试方法…我认为应该通过模拟测试tableview@ThatlazyiOSGuy웃 嗯,我想是的。你能分享做这件事的好方法吗
import UIKit
import XCTest

class MainViewControllerTests: XCTestCase {

    var controller: MainViewController!

    override func setUp() {
        super.setUp()

        controller = MainViewController()
        controller.view.description
    }

    override func tearDown() {
        super.tearDown()
    }

    func testTableViewOutlet() {
        XCTAssertNotNil(controller.tableView)
    }

    func testTableViewCellForRowAtIndexPath() {
        let indexPath = NSIndexPath(forRow: 0, inSection: 0)

        let cell = controller.tableView(controller.tableView, cellForRowAtIndexPath: indexPath) as! CustomCell

        XCTAssertEqual(cell.countryLabel.text!, "USA")
        XCTAssertEqual(cell.capitalLabel.text!, "Washington, D.C.")
    }
}