0
0
iOS Swiftmobile~10 mins

Snapshot testing in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a snapshot test for a UIView named myView.

iOS Swift
func testMyViewSnapshot() {
  let myView = UIView()
  // Configure myView
  assertSnapshot(matching: myView, as: .[1])
}
Drag options to blanks, or click blank then click option'
Ajson
Btext
Cimage
Daudio
Attempts:
3 left
💡 Hint
Common Mistakes
Using text or json instead of image for UI snapshots.
2fill in blank
medium

Complete the code to set a fixed size for the view before snapshot testing.

iOS Swift
func testFixedSizeViewSnapshot() {
  let view = UIView(frame: CGRect(x: 0, y: 0, width: [1], height: 100))
  assertSnapshot(matching: view, as: .image)
}
Drag options to blanks, or click blank then click option'
A200
B50
C0
D-100
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative width causing empty snapshots.
3fill in blank
hard

Fix the error in the snapshot test by completing the missing parameter for the snapshot strategy.

iOS Swift
func testLabelSnapshot() {
  let label = UILabel()
  label.text = "Hello"
  assertSnapshot(matching: label, as: .[1])
}
Drag options to blanks, or click blank then click option'
Atext
Bjson
Caudio
Dimage
Attempts:
3 left
💡 Hint
Common Mistakes
Using text or json instead of image for UILabel snapshots.
4fill in blank
hard

Fill both blanks to create a snapshot test that waits for the view to layout before capturing.

iOS Swift
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]))
}
Drag options to blanks, or click blank then click option'
Aimage
Btext
C0.99
D1.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using text snapshot or wrong precision values.
5fill in blank
hard

Fill all three blanks to create a snapshot test for a UIButton with a specific trait and size.

iOS Swift
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])
}
Drag options to blanks, or click blank then click option'
A100
B44
Cimage
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using text snapshot or wrong button sizes.