UISearchController 的 isActive 属性不能在单元测试中设置为 true



我正在为UITableViewController的数据源编写单元测试,该数据源有一个用于筛选结果的UISearchController。我需要测试NumberOfRowsInSection中的逻辑,这样当搜索控制器处于活动状态时,数据源会返回筛选数组中的计数,而不是正常数组中的。

该函数通过检查搜索控制器的"isActive"是否为真/假来控制此操作。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchController.isActive ? filteredResults.count : results.count
}

所以我的单元测试是这样写的

func testNumberOfRowsInSection() {
XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, dataSource.tableView(tableView, numberOfRowsInSection: 0))

searchController.isActive = true
XCTAssertTrue(searchController.isActive) // Fails right after setting it true
XCTAssertEqual(0, dataSource.tableView(tableView, numberOfRowsInSection: 0)) // Fails

searchController.isActive = false
XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, dataSource.tableView(tableView, numberOfRowsInSection: 0))
}

因此,在单元测试中将"isActive"属性设置为true后,它不会立即保持为true。这很奇怪,因为在应用程序的正常运行过程中,我可以在视图控制器中将其设置为true,并且它保持活动状态。

override func viewDidLoad() {
super.viewDidLoad()

// ...

searchController.isActive = true // Makes search bar immediately visible

// ...
}

文档显示它是一个可设置的属性,那么为什么在单元测试中不将其设置为true呢?我也试着把它连接到导航栏上,但没有改变任何东西。如果我不能在单元测试中这样测试它,我将不得不嘲笑它的功能,这会很烦人,因为测试它应该很简单。

更新示例:

class MovieSearchDataSourceTests: XCTestCase {
private var window: UIWindow!
private var controller: UITableViewController!
private var searchController: UISearchController!
private var sut: MovieSearchDataSource!
override func setUp() {
window = UIWindow()
controller = UITableViewController()
searchController = UISearchController()

sut = MovieSearchDataSource(tableView: controller.tableView,
searchController: searchController,
movies: Array(repeating: Movie.test, count: 4))

window.rootViewController = UINavigationController(rootViewController: controller)
controller.navigationItem.searchController = searchController
}
override func tearDown() {
window = nil
controller = nil
searchController = nil
sut = nil
RunLoop.current.run(until: Date())
}
func testNumberOfRowsInSection() {
window.addSubview(controller.tableView)
controller.loadViewIfNeeded()

XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, sut.tableView(controller.tableView, numberOfRowsInSection: 0))

searchController.isActive = true
XCTAssertTrue(searchController.isActive)
XCTAssertEqual(0, sut.tableView(controller.tableView, numberOfRowsInSection: 0))

searchController.isActive = false
XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, sut.tableView(controller.tableView, numberOfRowsInSection: 0))
}

}

当我面临这样的挑战时,我会选择两种工具:

  1. 执行运行循环
  2. 将视图控制器的视图放入真实窗口

我写了一个不起作用的单元测试,然后尝试了这些技巧。窗户把戏奏效了。

func test_canSetWhetherSearchControllerIsActive() throws {
putInViewHierarchy(sut)
sut.searchController.isActive = false
XCTAssertFalse(sut.searchController.isActive)
sut.searchController.isActive = true
XCTAssertTrue(sut.searchController.isActive)
}
func putInViewHierarchy(_ vc: UIViewController) {
let window = UIWindow()
window.addSubview(vc.view)
}

注意:当您使用窗口技巧时,视图控制器不会在测试结束时释放,除非我们也启动运行循环。这必须在测试功能结束后进行拆卸:

override func tearDownWithError() throws {
sut = nil
executeRunLoop()
try super.tearDownWithError()
}
func executeRunLoop() {
RunLoop.current.run(until: Date())
}

在没有让UIWindow工作之后,我的解决方案是用协议模拟UISearchController。

// Protocol in main project
protocol Activatable: AnyObject {
var isActive: Bool { get set }
}
extension UISearchController: Activatable { }
final class MovieSearchDataSource: NSObject {
private var movies: [Movie] = []
private var filteredMovies: [Movie] = []
private let tableView: UITableView
private let searchController: Activatable
init(tableView: UITableView, searchController: Activatable, movies: [Movie] = []) {
self.tableView = tableView
self.searchController = searchController
self.movies = movies
super.init()
}
}
extension MovieSearchDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchController.isActive ? filteredMovies.count : movies.count
}
}
// Unit test file
class MockSearchController: Activatable {
var isActive = false
}
class MovieSearchDataSourceTests: XCTestCase {
private var tableView: UITableView!
private var searchController: MockSearchController!
private var sut: MovieSearchDataSource!
override func setUp() {
tableView = UITableView()
searchController = MockSearchController()

sut = MovieSearchDataSource(tableView: tableView,
searchController: searchController,
movies: Array(repeating: Movie.test, count: 4))

}
override func tearDown() {
tableView = nil
searchController = nil
sut = nil
}
func testNumberOfRowsInSection() {
XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, sut.tableView(tableView, numberOfRowsInSection: 0))

searchController.isActive = true
XCTAssertTrue(searchController.isActive)
XCTAssertEqual(0, sut.tableView(tableView, numberOfRowsInSection: 0))

searchController.isActive = false
XCTAssertFalse(searchController.isActive)
XCTAssertEqual(4, sut.tableView(tableView, numberOfRowsInSection: 0))
}

}

最新更新