-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewControllerTests_XCTest.swift
More file actions
59 lines (51 loc) · 2.17 KB
/
TableViewControllerTests_XCTest.swift
File metadata and controls
59 lines (51 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ExpectToEventuallyEqual by Jon Reid (https://qualitycoding.org) and Steven Baker (https://stevenrbaker.com)
// Copyright 2023 Jonathan M. Reid. https://github.com/jonreid/ExpectToEventuallyEqual/blob/main/LICENSE.txt
// SPDX-License-Identifier: MIT
@testable import SampleApp
import ExpectToEventuallyEqual
import XCTest
@MainActor
final class TableViewControllerTests_XCTest: XCTestCase, Sendable {
private var sut: TableViewController!
private var tableDataSource: (any UITableViewDataSource)!
override func setUp() async throws {
try await super.setUp()
let book1 = SearchResult(trackName: "book 1", artistName: "author 1")
let book2 = SearchResult(trackName: "book 2", artistName: "Jon Reid")
let fakeSearchProvider = FakeSearchProvider(searchResults: book1, book2)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
sut = storyboard.instantiateViewController(identifier: String(describing: TableViewController.self))
sut.viewModel = ViewModel(fakeSearchProvider)
sut.loadViewIfNeeded()
tableDataSource = try XCTUnwrap(sut.tableView.dataSource)
}
override func tearDown() async throws {
sut = nil
tableDataSource = nil
try await super.tearDown()
}
func test_numberOfRows() async throws {
// begin-snippet: test-example
try await expectToEventuallyEqual(
actual: { tableDataSource.tableView(sut.tableView, numberOfRowsInSection: 0) },
expected: 2
)
// end-snippet
}
func test_secondRowShowsBookTitle() async throws {
try await expectToEventuallyEqual(
actual: { cellForRow(1).textLabel?.text ?? "" },
expected: "book 2"
)
}
func test_FAILURE_DEMONSTRATION_secondRowShowsBookAuthor() async throws {
try await expectToEventuallyEqual(
actual: { cellForRow(1).detailTextLabel?.text ?? "" },
expected: "Steven Baker"
)
}
private func cellForRow(_ row: Int) -> UITableViewCell {
let indexPath = IndexPath(row: row, section: 0)
return tableDataSource.tableView(sut.tableView, cellForRowAt: indexPath)
}
}