Home IOS Development ios – Bother Testing SwiftUI @State property Change in XCTest

ios – Bother Testing SwiftUI @State property Change in XCTest

0
ios – Bother Testing SwiftUI @State property Change in XCTest

[ad_1]

Downside Description

I’m dealing with a difficulty whereas attempting to check a SwiftUI view with a toggle motion utilizing XCTest. The check is failing, significantly within the final XCTAssertTrue assertion, and it appears to be associated to the asynchronous nature of SwiftUI updates.

Is there any solution to make the check work?

Code Snippets

Here is a simplified model of my code:

// XCTest Code
last class ToggleTests: XCTestCase {
    func testToggleAction() {
        // Given
        let view = ToggleView()
        XCTAssertFalse(view.toggle)
        // When
        view.handleToggleAction()
        // Then
        XCTAssertTrue(view.toggle)
    }
}

// SwiftUI View
struct ToggleView {
    @State personal(set) var toggle: Bool = false

    func handleToggleAction() {
        self.toggle = true
    }
}

extension ToggleView: View {
    var physique: some View {
        Button("Toggle") {
            self.handleToggleAction()
        }
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here