Complete the code to create a snapshot test for a UIView named myView.
func testMyViewSnapshot() {
let myView = UIView()
// Configure myView
assertSnapshot(matching: myView, as: .[1])
}The snapshot test compares the view's rendered image. Use image as the snapshot format.
Complete the code to set a fixed size for the view before snapshot testing.
func testFixedSizeViewSnapshot() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: [1], height: 100))
assertSnapshot(matching: view, as: .image)
}Setting width to 200 gives the view a visible size for snapshot testing.
Fix the error in the snapshot test by completing the missing parameter for the snapshot strategy.
func testLabelSnapshot() {
let label = UILabel()
label.text = "Hello"
assertSnapshot(matching: label, as: .[1])
}Labels are snapshot tested as images to capture their visual appearance.
Fill both blanks to create a snapshot test that waits for the view to layout before capturing.
func testLayoutSnapshot() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.setNeedsLayout()
view.layoutIfNeeded()
assertSnapshot(matching: view, as: .[1](precision: [2]))
}Use image snapshot with a precision of 0.99 for slight rendering differences tolerance.
Fill all three blanks to create a snapshot test for a UIButton with a specific trait and size.
func testButtonSnapshot() {
let button = UIButton(type: .system)
button.setTitle("Tap", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: [1], height: [2])
assertSnapshot(matching: button, as: .[3])
}Set button width to 100, height to 44 (standard height), and snapshot as image.