When a view has a .help modifier (macOS for help tool-tips), it causes ViewInspector’s find method to fail.
Removing the .help modifier lets the test pass, but that’s not an acceptable solution.
MyView.swift:
import SwiftUI
struct MyView: View {
var didAppear: ((Self) -> Void)?
// various properties ...
var body: some View {
VStack {
Button("My Button") { self.buttonAction() }
.accessibilityIdentifier("my button")
.help("The help text for my button.") // ⬅️ the modifier that causes problems
}
.onAppear {
self.didAppear?(self)
}
}
// other methods and stuff ...
}
MyViewTests.swift
@testable import MyApp
import SwiftUI
import ViewInspector
import XCTest
final class MyViewTests: XCTestCase {
func test_whenTryToFind_shouldReturnButtonView() throws {
var sut = MyView()
// the following line reports an error, based on the `find` call in its closure:
// 🛑 failed - Search did not find a match. Possible blockers: HelpView<ModifiedContent<Button<Text>, AccessibilityAttachmentModifier>>
let appear = sut.on(\.didAppear) { view in
let button = try view.find(viewWithAccessibilityIdentifier: "my button").button()
try button.tap()
// my assertion
}
ViewHosting.host(view: sut)
wait(for: [appear], timeout: 0.1)
}
}
ViewInspector: 0.9.8 Xcode: 15.0.1 macOS: Sonoma 14.1.1 Processor: M1
When a view has a
.help
modifier (macOS for help tool-tips), it causes ViewInspector’sfind
method to fail.Removing the
.help
modifier lets the test pass, but that’s not an acceptable solution.MyView.swift:
MyViewTests.swift