Because of WatchKit API limitations ViewInspector currently cannot automatically host your views for asynchronous tests - you need to add this swift file to your watchOS app target (for projects created prior Xcode 14 - to watchOS extension target).
Then, add the appropriate code snippet, depending on your setup:
- Make sure to add
WKExtensionDelegate
and reference it in theApp
using@WKExtensionDelegateAdaptor
. - Add
let testViewSubject = TestViewSubject([])
as an instance variable to theExtensionDelegate
. - Add
.testable(extDelegate.testViewSubject)
to yourContentView
insideWindowGroup
.
The final code should look similar to this:
final class ExtensionDelegate: NSObject, WKExtensionDelegate {
let testViewSubject = TestViewSubject([]) // #2
}
@main
struct MyWatchOSApp: App {
@WKExtensionDelegateAdaptor(ExtensionDelegate.self) var extDelegate // #1
var body: some Scene {
WindowGroup {
ContentView()
.testable(extDelegate.testViewSubject) // #3
}
}
}
- Make sure your root interface controller is a
WKHostingController
descendant. - Add
let testViewSubject = TestViewSubject([])
as an instance variable to your root interface controller. - Add
.testable(testViewSubject)
to yourContentView
- Replace
var body: ContentView
withvar body: RootView<ContentView>
- Replace
WKHostingController<ContentView>
withWKHostingController<RootView<ContentView>>
The final code should look similar to this:
final class RootInterfaceController: WKHostingController<RootView<ContentView>> { // #1 , #5
let testViewSubject = TestViewSubject([]) // #2
override var body: RootView<ContentView> { // #4
return ContentView()
.testable(testViewSubject) // #3
}
}
struct ContentView: View {
var body: some View {
Text("Hi")
}
}
The proposed code snippets are using conditional compilation
to minimize the footprint in the main build target.
The condition #if !(os(watchOS) && DEBUG)
fully disintegrates the test code in Release
builds and replaces it with these primitives:
typealias RootView<T> = T
typealias TestViewSubject = Set<Int>
extension View {
@inline(__always)
func testable(_ injector: TestViewSubject) -> Self {
self
}
}