【发布时间】:2020-07-31 20:28:32
【问题描述】:
我有一个 SwiftUI 视图层次结构,其中包含一个使用 .environment() 注入的自定义类的实例,类似于以下内容:
struct ContentView: View {
// Pointer to the AppStateController passed in .environment()
@EnvironmentObject var appStateController: AppStateController
var body: some View {
VStack(spacing: 0) {
TitleView()
.modifier(TitleStyle())
.environmentObject(appStateController)
Spacer()
}
}
}
struct TitleView: View {
@EnvironmentObject var appStateController: AppStateController
var body: some View {
Button(action: {
self.appStateController.isPlaying.toggle()
}, label: {
if self.appStateController.isPlaying {
Image(systemName: "stop.circle")
.opacity(self.appStateController.isPlayable ? 1.0 : 0.5)
.accessibility(label: Text("stop"))
}
else {
Image(systemName: "play.circle")
.opacity(self.appStateController.isPlayable ? 1.0 : 0.5)
.accessibility(label: Text("play"))
}
})
}
}
在 TitleView 上有一堆按钮,它们的动作会改变 appStateController 中的 @Published 值。这些按钮在被点击时也会改变它们的标签(图标)。
我刚刚开始进行 UI 单元测试,我已经完成了测试按钮点击更改图标(通过搜索按钮并检查其可访问性标签)的工作,但我也想通过检查 appStateController.isPlaying 布尔值来断言该操作实际上做了某事 - 有效地测试我的操作:{} 闭包是否符合我的需要。
我似乎找不到任何文档告诉我如何通过视图层次结构找到注入的 appStateController 的引用并检查其中的属性。这可能吗?如果可以,有谁知道我在哪里可以找到一些关于这样做的文档/博客文章?
【问题讨论】:
标签: swift swiftui xctest xctestcase