如何将测试添加到与@fetchrequest一起使用Coredata的SwiftUI视图中?

发布于 2025-02-11 20:56:20 字数 725 浏览 1 评论 0原文

我正在尝试为SwiftUI视图添加快照测试,该测试正在使用@fetchrequest属性包装器使用核心数据来驱动UI。我想在UI中添加一些逻辑,并想进行一些快照测试以覆盖它。

以下是我要测试的Swiftui视图的片段示例。

struct TeamView: View {
    @Environment(\.managedObjectContext) var viewContext
    
    @FetchRequest(sortDescriptors: [SortDescriptor(\.name)])
    private var teams: FetchedResults<Team>
    
    var body: some View {
        NavigationView {
            List {
                ForEach(teams, id: \.id) { team in
                   Text(team.name)
                }
            }
            .navigationTitle(Text(TabItem.team.title))
        }
    }
}

我如何创建team对象,然后可以在teamView中使用以启用测试?

I am trying to add a snapshot test for a SwiftUI view that is using Core Data to drive the UI via the @FetchRequest property wrapper. I would like to add a small bit of logic within the UI and would like to have some snapshot tests to cover it.

Below is a snippet example of the SwiftUI view that I am trying to test.

struct TeamView: View {
    @Environment(\.managedObjectContext) var viewContext
    
    @FetchRequest(sortDescriptors: [SortDescriptor(\.name)])
    private var teams: FetchedResults<Team>
    
    var body: some View {
        NavigationView {
            List {
                ForEach(teams, id: \.id) { team in
                   Text(team.name)
                }
            }
            .navigationTitle(Text(TabItem.team.title))
        }
    }
}

Any ideas how I can create Team Objects that can then be used within TeamView to enable testing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文