Combineを使ったコードのテストをSchedulerで操る方法とその仕組み

10.6K Views

October 04, 21

スライド概要

iOSDC2021 で発表した内容です

profile-image

iOS エンジニアをやっています。

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

(ダウンロード不可)

関連スライド

各ページのテキスト
1.

Combine Scheduler iOSDC Japan 2021

2.

iOS & 2 Sansan @kalupas0930 2

3.

Combine ViewModel + Combine combine-schedulers combine-schedulers 3

4.

https://github.com/kalupas226/iOSDC2021SampleApp 4

5.

Search 5

6.

Model Response GitHubRepository.swift / Response body struct GitHubRepository: Decodable, Identifiable, Equatable { { let id: Int let fullName: String "items": [ { "id": 3081286, "full_name": "iOSDC" }, { "id": 3081287, "full_name": "iOSDC2021" }, // ... ] } extension GitHubRepository { private enum CodingKeys: String, CodingKey { case id case fullName = "full_name" } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) } id = try container.decode(Int.self, forKey: .id) fullName = try container.decode(String.self, forKey: .fullName) } } struct GitHubRepositoryList: Decodable { let items: [GitHubRepository] } 6

7.
[beta]
Model API GitHubAPIClient.swift // interface struct GitHubAPIClient { var searchRepository: (String) -> AnyPublisher<GitHubRepositoryList, Never> } // extension GitHubAPIClient { // static GitHubAPIClient `.live` // ex) GitHubListView(viewModel: GitHubViewModel(gitHubAPIClient: .live)) static let live = Self( searchRepository: { searchWord in URLSession.shared.dataTaskPublisher( for: URL(string: "https://api.github.com/search/repositories?q=\(searchWord)")! ) .map { $0.data } .decode(type: GitHubRepositoryList.self, decoder: JSONDecoder()) .replaceError(with: .init(items: [])) // .eraseToAnyPublisher() } ) } 7
8.

ViewModel GitHubViewModel.swift 8

9.

View GitHubListView.swift 9

11.

SearchButton iOSDC2021SampleAppTests.swift 11

12.

SearchButton iOSDC2021SampleAppTests.swift 12

14.

publish values from the main thread GitHubViewModel.swift 14

15.

DispatchQueue.main receive GitHubViewModel.swift 15

17.

iOSDC2021SampleAppTests.swift 17

18.

wait iOSDC2021SampleAppTests.swift 18

20.

GitHubViewModel.swift 20

21.

GitHubViewModel.swift 21

24.

iOSDC2021SampleAppTests.swift 24

25.

iOSDC2021SampleAppTests.swift 25

26.

0.3s GitHubViewModel.swift 26

27.

iOSDC2021SampleAppTests.swift 27

28.

wait 28

29.

0.3s 0.33s wait CI 0.33s 29

30.

combine-schedulers 30

31.

combine-schedulers Point-Free Combine Combine Scheduler protocol Scheduler AnyScheduler TestScheduler ImmediateScheduler Animated schedulers FailingScheduler UIScheduler Publishers.Timer 31

32.

combine-schedulers Point-Free Combine Combine Scheduler protocol Scheduler AnyScheduler TestScheduler ImmediateScheduler Animated schedulers FailingScheduler UIScheduler Publishers.Timer 32

33.

schedulers ViewModel combine-

34.

ViewModel GitHubViewModel.swift 34

35.

combine-schedulers Scheduler GitHubViewModel.swift 35

36.

ViewModel GitHubViewModel.swift 36

37.

combine-shcedulers Scheduler GitHubViewModel.swift 37

38.

Preview ViewModel initialize GitHubListView.swift 38

39.

GitHubListView.swift 39

41.

combine-schedulers

42.

iOSDC2021SampleAppTests.swift 42

43.

iOSDC2021SampleAppTests.swift 43

44.

iOSDC2021SampleAppTests.swift 44

45.

TestScheduler iOSDC2021SampleAppTests.swift 45

46.

TestScheduler iOSDC2021SampleAppTests.swift 46

47.

iOSDC2021SampleAppTests.swift 47

48.

iOSDC2021SampleAppTests.swift 48

50.

combine-schedulers 0.3s 0.33s wait 0.33s 50

51.

combine-schedulers 0.3s 0.33s wait 0.33s 51

52.

combine-schedulers ask the speaker

53.

, 53

54.

operator Combine operator 54

55.

protocol 55

56.

Scheduler Scheduler option Scheduler 56

57.

protocol 57

58.

Scheduler Scheduler 58

59.

protocol 59

60.

3 function 60

61.

Scheduler protocol Scheduler

62.

Apple DispatchQueue ImmediateScheduler RunLoop OperationQueue 62

63.

DispatchQueue 63

64.

Scheduler combine-shcedulers

65.

Test Scheduler Scheduler Test combine-schedulers protocol 2 associatedtype 2 property 3 function interval 65

66.

Test Scheduler Scheduler Test combine-schedulers protocol 2 associatedtype 2 property 3 function interval 66

67.

Scheduler Scheduler MyTestScheduler  typealias 67

68.

Generics associatedtype property Scheduler DispatchQueue ImmediateScheduler RunLoop OperationQueue Scheduler TestScheduler Generics 68

69.

initializer MyTestScheduler.swift 69

70.

1 function MyTestScheduler.swift 1 function 70

71.

MyTestScheduler.swift 71

72.

MyTestScheduler.swift -> schedule 72

73.

MyTestScheduler MyTestScheduler.swift Combine 73

74.

schedule advance iOSDC2021SampleAppTests.swift 74

75.

MyTestScheduler MyTestScheduler.swift 75

76.

DispatchQueue.myTest iOSDC2021SampleAppTests.swift 76

77.

Publisher MyTestScheduler iOSDC2021SampleAppTests.swift 77

78.

2 function MyTestScheduler.swift 2 function 78

79.

MyTestScheduler.swift 79

80.

date MyTestScheduler.swift 80

81.

advance date MyTestScheduler.swift 81

82.

2 function iOSDC2021SampleAppTests.swift 82

83.

(5000s) iOSDC2021SampleAppTests.swift 83

85.

3 function MyTestScheduler.swift 3 function interval 85

86.

MyTestScheduler.swift 86

87.

interval 87

88.

MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 88

89.

MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 89

90.

MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 90

91.

MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 91

92.

0.5s 1s MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 92

93.

1s MyTestScheduler.scheduled [] s.schedule(after: scheduler.now, interval: 1) [scheduleAction(for: now), now)] s.advance() [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 1), now + 1)] s.advance(.milliseconds(500)) [scheduleAction(for: now + 2), now + 2)] 93

94.

iOSDC2021SampleAppTests.swift 94

95.

iOSDC2021SampleAppTests.swift 95

96.

interval MyTestScheduler.swift 96

97.

MyTestScheduler.swift 97

98.

interval iOSDC2021SampleAppTests.swift 98

99.

interval iOSDC2021SampleAppTests.swift 99

100.

iOSDC2021SampleAppTests.swift 100

101.

MyTestScheduler.swift 101

102.

MyTestScheduler.swift 102

103.

iOSDC2021SampleAppTests.swift 103

104.

interval iOSDC2021SampleAppTests.swift Date interval schedule function 104

105.

MyTestScheduler.swift 105

106.

1 2 function MyTestScheduler.swift 106

107.

3 function MyTestScheduler.swift 107

108.

iOSDC2021SampleAppTests.swift 108

109.

interval iOSDC2021SampleAppTests.swift 109

110.

interval iOSDC2021SampleAppTests.swift 110

111.

AnyCancellable MyTestScheduler.swift 111

112.

MyTestScheduler.swift 112

113.

iOSDC2021SampleAppTests.swift 113

114.

combine-schedulers

115.

Combine operator , , , … wait wait combine-schedulers combine-schedulers Protocol Scheduler Test animation Generics 115

116.

GitHub pointfreeco / combine-schedulers tcldr / Entwine Point-Free Combine Schedulers: Testing Time Combine Schedulers: Controlling Time Combine Schedulers: Erasing Time Apple Developer Documentation Scheduler 116

117.

117