@@ -8,18 +8,15 @@ class SpeechRecognitionTests: XCTestCase {
8
8
let recognitionTaskSubject = Signal < SpeechRecognitionResult , SpeechClient . Error > . pipe ( )
9
9
10
10
func testDenyAuthorization( ) {
11
- var speechClient = SpeechClient . unimplemented
12
- speechClient. requestAuthorization = { Effect ( value: . denied) }
13
-
14
11
let store = TestStore (
15
12
initialState: AppState ( ) ,
16
13
reducer: appReducer,
17
- environment: AppEnvironment (
18
- mainQueue: ImmediateScheduler ( ) ,
19
- speechClient: speechClient
20
- )
14
+ environment: . unimplemented
21
15
)
22
16
17
+ store. environment. mainQueue = ImmediateScheduler ( )
18
+ store. environment. speechClient. requestAuthorization = { Effect ( value: . denied) }
19
+
23
20
store. send ( . recordButtonTapped) {
24
21
$0. isRecording = true
25
22
}
@@ -32,50 +29,42 @@ class SpeechRecognitionTests: XCTestCase {
32
29
)
33
30
)
34
31
$0. isRecording = false
35
- $0. speechRecognizerAuthorizationStatus = . denied
36
32
}
37
33
}
38
34
39
35
func testRestrictedAuthorization( ) {
40
- var speechClient = SpeechClient . unimplemented
41
- speechClient. requestAuthorization = { Effect ( value: . restricted) }
42
-
43
36
let store = TestStore (
44
37
initialState: AppState ( ) ,
45
38
reducer: appReducer,
46
- environment: AppEnvironment (
47
- mainQueue: ImmediateScheduler ( ) ,
48
- speechClient: speechClient
49
- )
39
+ environment: . unimplemented
50
40
)
51
41
42
+ store. environment. mainQueue = ImmediateScheduler ( )
43
+ store. environment. speechClient. requestAuthorization = { Effect ( value: . restricted) }
44
+
52
45
store. send ( . recordButtonTapped) {
53
46
$0. isRecording = true
54
47
}
55
48
store. receive ( . speechRecognizerAuthorizationStatusResponse( . restricted) ) {
56
49
$0. alert = AlertState ( title: TextState ( " Your device does not allow speech recognition. " ) )
57
50
$0. isRecording = false
58
- $0. speechRecognizerAuthorizationStatus = . restricted
59
51
}
60
52
}
61
53
62
54
func testAllowAndRecord( ) {
63
- var speechClient = SpeechClient . unimplemented
64
- speechClient. finishTask = {
65
- . fireAndForget { self . recognitionTaskSubject. input. sendCompleted ( ) }
66
- }
67
- speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
68
- speechClient. requestAuthorization = { Effect ( value: . authorized) }
69
-
70
55
let store = TestStore (
71
56
initialState: AppState ( ) ,
72
57
reducer: appReducer,
73
- environment: AppEnvironment (
74
- mainQueue: ImmediateScheduler ( ) ,
75
- speechClient: speechClient
76
- )
58
+ environment: . unimplemented
77
59
)
78
60
61
+ store. environment. mainQueue = ImmediateScheduler ( )
62
+ store. environment. speechClient. finishTask = {
63
+ . fireAndForget { self . recognitionTaskSubject. input. sendCompleted ( ) }
64
+ }
65
+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
66
+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
67
+
79
68
let result = SpeechRecognitionResult (
80
69
bestTranscription: Transcription (
81
70
formattedString: " Hello " ,
@@ -92,9 +81,7 @@ class SpeechRecognitionTests: XCTestCase {
92
81
$0. isRecording = true
93
82
}
94
83
95
- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
96
- $0. speechRecognizerAuthorizationStatus = . authorized
97
- }
84
+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
98
85
99
86
self . recognitionTaskSubject. input. send ( value: result)
100
87
store. receive ( . speech( . success( result) ) ) {
@@ -108,26 +95,21 @@ class SpeechRecognitionTests: XCTestCase {
108
95
}
109
96
110
97
func testAudioSessionFailure( ) {
111
- var speechClient = SpeechClient . unimplemented
112
- speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
113
- speechClient. requestAuthorization = { Effect ( value: . authorized) }
114
-
115
98
let store = TestStore (
116
99
initialState: AppState ( ) ,
117
100
reducer: appReducer,
118
- environment: AppEnvironment (
119
- mainQueue: ImmediateScheduler ( ) ,
120
- speechClient: speechClient
121
- )
101
+ environment: . unimplemented
122
102
)
123
103
104
+ store. environment. mainQueue = ImmediateScheduler ( )
105
+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
106
+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
107
+
124
108
store. send ( . recordButtonTapped) {
125
109
$0. isRecording = true
126
110
}
127
111
128
- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
129
- $0. speechRecognizerAuthorizationStatus = . authorized
130
- }
112
+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
131
113
132
114
self . recognitionTaskSubject. input. send ( error: . couldntConfigureAudioSession)
133
115
store. receive ( . speech( . failure( . couldntConfigureAudioSession) ) ) {
@@ -138,26 +120,21 @@ class SpeechRecognitionTests: XCTestCase {
138
120
}
139
121
140
122
func testAudioEngineFailure( ) {
141
- var speechClient = SpeechClient . unimplemented
142
- speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
143
- speechClient. requestAuthorization = { Effect ( value: . authorized) }
144
-
145
123
let store = TestStore (
146
124
initialState: AppState ( ) ,
147
125
reducer: appReducer,
148
- environment: AppEnvironment (
149
- mainQueue: ImmediateScheduler ( ) ,
150
- speechClient: speechClient
151
- )
126
+ environment: . unimplemented
152
127
)
153
128
129
+ store. environment. mainQueue = ImmediateScheduler ( )
130
+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. output. producer }
131
+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
132
+
154
133
store. send ( . recordButtonTapped) {
155
134
$0. isRecording = true
156
135
}
157
136
158
- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
159
- $0. speechRecognizerAuthorizationStatus = . authorized
160
- }
137
+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
161
138
162
139
self . recognitionTaskSubject. input. send ( error: . couldntStartAudioEngine)
163
140
store. receive ( . speech( . failure( . couldntStartAudioEngine) ) ) {
@@ -167,3 +144,10 @@ class SpeechRecognitionTests: XCTestCase {
167
144
self . recognitionTaskSubject. input. sendCompleted ( )
168
145
}
169
146
}
147
+
148
+ extension AppEnvironment {
149
+ static let unimplemented = Self (
150
+ mainQueue: UnimplementedScheduler ( ) ,
151
+ speechClient: . unimplemented
152
+ )
153
+ }
0 commit comments