Swift Testing for Unit Tests PART 1 (#5119)

* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment

more tests migration

Cleaned the code manually to establish some good patterns

more code improvements

some more code improvements

removed empty tests

update project

* more pr suggestions and cleanups

* removed the TestSetup pattern

* fixing claude not reusing tests

* pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
Mauro
2026-02-19 16:20:47 +01:00
committed by GitHub
parent fc03e05697
commit 7982aef855
118 changed files with 4630 additions and 4129 deletions
+25 -19
View File
@@ -9,10 +9,11 @@
import Combine
@testable import ElementX
import Foundation
import XCTest
import Testing
@MainActor
class AudioRecorderStateTests: XCTestCase {
@Suite
struct AudioRecorderStateTests {
private var audioRecorderState: AudioRecorderState!
private var audioRecorderMock: AudioRecorderMock!
@@ -20,7 +21,7 @@ class AudioRecorderStateTests: XCTestCase {
private var audioRecorderActions: AnyPublisher<AudioRecorderAction, Never> {
audioRecorderActionsSubject.eraseToAnyPublisher()
}
private func buildAudioRecorderMock() -> AudioRecorderMock {
let audioRecorderMock = AudioRecorderMock()
audioRecorderMock.isRecording = false
@@ -30,34 +31,38 @@ class AudioRecorderStateTests: XCTestCase {
return audioRecorderMock
}
override func setUp() async throws {
init() async {
audioRecorderActionsSubject = .init()
audioRecorderState = AudioRecorderState()
audioRecorderMock = buildAudioRecorderMock()
}
func testAttach() {
@Test
func attach() {
audioRecorderState.attachAudioRecorder(audioRecorderMock)
XCTAssertEqual(audioRecorderState.recordingState, .stopped)
#expect(audioRecorderState.recordingState == .stopped)
}
func testDetach() async {
@Test
mutating func detach() async {
audioRecorderState.attachAudioRecorder(audioRecorderMock)
audioRecorderMock.isRecording = true
await audioRecorderState.detachAudioRecorder()
XCTAssert(audioRecorderMock.stopRecordingCalled)
XCTAssertEqual(audioRecorderState.recordingState, .stopped)
#expect(audioRecorderMock.stopRecordingCalled)
#expect(audioRecorderState.recordingState == .stopped)
}
func testReportError() {
XCTAssertEqual(audioRecorderState.recordingState, .stopped)
@Test
mutating func reportError() {
#expect(audioRecorderState.recordingState == .stopped)
audioRecorderState.reportError()
XCTAssertEqual(audioRecorderState.recordingState, .error)
#expect(audioRecorderState.recordingState == .error)
}
func testHandlingAudioRecorderActionDidStartRecording() async throws {
@Test
func handlingAudioRecorderActionDidStartRecording() async throws {
audioRecorderState.attachAudioRecorder(audioRecorderMock)
let deferred = deferFulfillment(audioRecorderState.$recordingState) { action in
switch action {
case .recording:
@@ -69,12 +74,13 @@ class AudioRecorderStateTests: XCTestCase {
audioRecorderActionsSubject.send(.didStartRecording)
try await deferred.fulfill()
XCTAssertEqual(audioRecorderState.recordingState, .recording)
#expect(audioRecorderState.recordingState == .recording)
}
func testHandlingAudioPlayerActionDidStopRecording() async throws {
@Test
func handlingAudioPlayerActionDidStopRecording() async throws {
audioRecorderState.attachAudioRecorder(audioRecorderMock)
let deferred = deferFulfillment(audioRecorderState.$recordingState) { action in
switch action {
case .stopped:
@@ -88,6 +94,6 @@ class AudioRecorderStateTests: XCTestCase {
try await deferred.fulfill()
// The state is expected to be .readyToPlay
XCTAssertEqual(audioRecorderState.recordingState, .stopped)
#expect(audioRecorderState.recordingState == .stopped)
}
}