@@ -8,18 +8,15 @@ class SpeechRecognitionTests: XCTestCase {
88 let recognitionTaskSubject = PassthroughSubject < SpeechRecognitionResult , SpeechClient . Error > ( )
99
1010 func testDenyAuthorization( ) {
11- var speechClient = SpeechClient . unimplemented
12- speechClient. requestAuthorization = { Effect ( value: . denied) }
13-
1411 let store = TestStore (
1512 initialState: AppState ( ) ,
1613 reducer: appReducer,
17- environment: AppEnvironment (
18- mainQueue: . immediate,
19- speechClient: speechClient
20- )
14+ environment: . unimplemented
2115 )
2216
17+ store. environment. mainQueue = . immediate
18+ store. environment. speechClient. requestAuthorization = { Effect ( value: . denied) }
19+
2320 store. send ( . recordButtonTapped) {
2421 $0. isRecording = true
2522 }
@@ -32,50 +29,42 @@ class SpeechRecognitionTests: XCTestCase {
3229 )
3330 )
3431 $0. isRecording = false
35- $0. speechRecognizerAuthorizationStatus = . denied
3632 }
3733 }
3834
3935 func testRestrictedAuthorization( ) {
40- var speechClient = SpeechClient . unimplemented
41- speechClient. requestAuthorization = { Effect ( value: . restricted) }
42-
4336 let store = TestStore (
4437 initialState: AppState ( ) ,
4538 reducer: appReducer,
46- environment: AppEnvironment (
47- mainQueue: . immediate,
48- speechClient: speechClient
49- )
39+ environment: . unimplemented
5040 )
5141
42+ store. environment. mainQueue = . immediate
43+ store. environment. speechClient. requestAuthorization = { Effect ( value: . restricted) }
44+
5245 store. send ( . recordButtonTapped) {
5346 $0. isRecording = true
5447 }
5548 store. receive ( . speechRecognizerAuthorizationStatusResponse( . restricted) ) {
5649 $0. alert = AlertState ( title: TextState ( " Your device does not allow speech recognition. " ) )
5750 $0. isRecording = false
58- $0. speechRecognizerAuthorizationStatus = . restricted
5951 }
6052 }
6153
6254 func testAllowAndRecord( ) {
63- var speechClient = SpeechClient . unimplemented
64- speechClient. finishTask = {
65- . fireAndForget { self . recognitionTaskSubject. send ( completion: . finished) }
66- }
67- speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
68- speechClient. requestAuthorization = { Effect ( value: . authorized) }
69-
7055 let store = TestStore (
7156 initialState: AppState ( ) ,
7257 reducer: appReducer,
73- environment: AppEnvironment (
74- mainQueue: . immediate,
75- speechClient: speechClient
76- )
58+ environment: . unimplemented
7759 )
7860
61+ store. environment. mainQueue = . immediate
62+ store. environment. speechClient. finishTask = {
63+ . fireAndForget { self . recognitionTaskSubject. send ( completion: . finished) }
64+ }
65+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
66+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
67+
7968 let result = SpeechRecognitionResult (
8069 bestTranscription: Transcription (
8170 formattedString: " Hello " ,
@@ -92,9 +81,7 @@ class SpeechRecognitionTests: XCTestCase {
9281 $0. isRecording = true
9382 }
9483
95- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
96- $0. speechRecognizerAuthorizationStatus = . authorized
97- }
84+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
9885
9986 self . recognitionTaskSubject. send ( result)
10087 store. receive ( . speech( . success( result) ) ) {
@@ -108,26 +95,21 @@ class SpeechRecognitionTests: XCTestCase {
10895 }
10996
11097 func testAudioSessionFailure( ) {
111- var speechClient = SpeechClient . unimplemented
112- speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
113- speechClient. requestAuthorization = { Effect ( value: . authorized) }
114-
11598 let store = TestStore (
11699 initialState: AppState ( ) ,
117100 reducer: appReducer,
118- environment: AppEnvironment (
119- mainQueue: . immediate,
120- speechClient: speechClient
121- )
101+ environment: . unimplemented
122102 )
123103
104+ store. environment. mainQueue = . immediate
105+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
106+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
107+
124108 store. send ( . recordButtonTapped) {
125109 $0. isRecording = true
126110 }
127111
128- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
129- $0. speechRecognizerAuthorizationStatus = . authorized
130- }
112+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
131113
132114 self . recognitionTaskSubject. send ( completion: . failure( . couldntConfigureAudioSession) )
133115 store. receive ( . speech( . failure( . couldntConfigureAudioSession) ) ) {
@@ -138,26 +120,21 @@ class SpeechRecognitionTests: XCTestCase {
138120 }
139121
140122 func testAudioEngineFailure( ) {
141- var speechClient = SpeechClient . unimplemented
142- speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
143- speechClient. requestAuthorization = { Effect ( value: . authorized) }
144-
145123 let store = TestStore (
146124 initialState: AppState ( ) ,
147125 reducer: appReducer,
148- environment: AppEnvironment (
149- mainQueue: . immediate,
150- speechClient: speechClient
151- )
126+ environment: . unimplemented
152127 )
153128
129+ store. environment. mainQueue = . immediate
130+ store. environment. speechClient. startTask = { _ in self . recognitionTaskSubject. eraseToEffect ( ) }
131+ store. environment. speechClient. requestAuthorization = { Effect ( value: . authorized) }
132+
154133 store. send ( . recordButtonTapped) {
155134 $0. isRecording = true
156135 }
157136
158- store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) ) {
159- $0. speechRecognizerAuthorizationStatus = . authorized
160- }
137+ store. receive ( . speechRecognizerAuthorizationStatusResponse( . authorized) )
161138
162139 self . recognitionTaskSubject. send ( completion: . failure( . couldntStartAudioEngine) )
163140 store. receive ( . speech( . failure( . couldntStartAudioEngine) ) ) {
@@ -167,3 +144,10 @@ class SpeechRecognitionTests: XCTestCase {
167144 self . recognitionTaskSubject. send ( completion: . finished)
168145 }
169146}
147+
148+ extension AppEnvironment {
149+ static let unimplemented = Self (
150+ mainQueue: . unimplemented,
151+ speechClient: . unimplemented
152+ )
153+ }
0 commit comments