I have a visionOS app using immersive space with RealityView. The app adds RealityKit entities to the app's Scene
instance, and uses raycast to find CollisionCastHits.
I want now to write a unit test to check if the app finds the right hits.
To do so, I have to access the Scene
instance to add entities, and to check if they are hit by scene.raycast.
But how can I access the scene instance?
I can access it e.g. after creating the RealityView
via its content
parameter, or via @Environment(\.realityKitScene).
But this seems not to be possible in a unit test.
I tried the following test function:
@MainActor @Test func test() async throws {
var scene: RealityKit.Scene?
await withCheckedContinuation { continuation in
_ = RealityView(make: { content in
print("make")
let entity = Entity()
content.add(entity)
scene = entity.scene
continuation.resume()
})
}
#expect(scene != nil)
}
But this logs
◇ Test test() started. SWIFT TASK CONTINUATION MISUSE: test() leaked its continuation!
The reason is apparently that the make
closure of RealityView
is only called when SwiftUI calls it within the body
of a SwiftUI View.
So, is it possible at all to access the app's scene i a unit test?