Consider the following struct
struct MainView: View {
// The `Model` the `User` shall be read from
@EnvironmentObject private var model: Model
var body: some View {
TabView {
AccountsOverview()
.tabItem {
Image(systemName: "rectangle.stack")
Text("Accounts")
}
TransactionOverview()
.tabItem {
Image(systemName: "list.dash")
Text("Transactions")
}
}
}
}
How to write a XCTest that tests if the second tab item
Appreciate your input!
Thing you are trying to test/validate is a View
and for testing View
one should use either SnapshotTests
or UITests
. In here I would suggest to start with SnapshotTests
to verify your view component.
Considering following is your view you want to test
class Model: ObservableObject {}
struct MainView: View {
@EnvironmentObject private var model: Model
var body: some View {
TabView {
AccountsOverview()
.tabItem {
Image(systemName: "rectangle.stack")
Text("Accounts")
}
TransactionOverview()
.tabItem {
Image(systemName: "list.dash")
Text("Transactions")
}
}
}
}
struct AccountsOverview: View {
var body: some View {
Text("AccountsOverview")
}
}
struct TransactionOverview: View {
var body: some View {
Text("TransactionOverview")
}
}
Now there are many frameworks available to achieve snapshot test but you can use SnapshotTesting library from PointFree. Refer this article from Kodeco to start with SnapshotTesting.
Following is the snapshot test for you View:
import XCTest
import SnapshotTesting
@testable import <your project target>
class MainViewSnapshotTests: XCTestCase {
func testExample() throws {
let view = MainView()
assertSnapshot(matching: view, as: .image)
}
}
Following is the reference snapshot generated using above snapshot test.