0
0
iOS Swiftmobile~20 mins

Snapshot testing in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What will this snapshot test verify?
Given this Swift snapshot test code, what UI aspect does it verify?
iOS Swift
func testButtonSnapshot() {
  let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
  button.setTitle("Tap me", for: .normal)
  assertSnapshot(matching: button, as: .image)
}
AIt checks if the button's title is correctly set to "Tap me".
BIt verifies the button's accessibility label is set.
CIt tests if the button responds to taps correctly.
DIt verifies the button's visual appearance matches the saved image snapshot.
Attempts:
2 left
💡 Hint
Snapshot tests compare the UI's look to a saved image.
lifecycle
intermediate
2:00remaining
When should snapshot tests be updated?
You run a snapshot test and it fails because the UI changed intentionally. What is the correct next step?
AUpdate the saved snapshot to the new UI image after verifying the change is expected.
BIgnore the failure and keep the old snapshot as is.
CDelete the snapshot test to avoid future failures.
DChange the UI code back to match the old snapshot without review.
Attempts:
2 left
💡 Hint
Snapshot tests should reflect intentional UI updates.
🔧 Debug
advanced
2:00remaining
Why does this snapshot test fail with a size mismatch?
This snapshot test fails with a size mismatch error. What is the most likely cause?
iOS Swift
func testLabelSnapshot() {
  let label = UILabel()
  label.text = "Hello"
  assertSnapshot(matching: label, as: .image)
}
AThe label text "Hello" is too long for the snapshot.
BThe label has no frame or constraints, so its size is zero when snapshot is taken.
CThe snapshot library does not support UILabel components.
DThe test is missing a call to set the label's accessibility identifier.
Attempts:
2 left
💡 Hint
UI components need a size to render properly in snapshot tests.
🧠 Conceptual
advanced
2:00remaining
What is a key limitation of snapshot testing?
Which of these is a main limitation of snapshot testing in mobile apps?
AIt automatically fixes UI bugs without developer input.
BIt requires writing complex UI interaction code.
CIt cannot detect functional bugs that do not affect UI appearance.
DIt replaces the need for unit tests entirely.
Attempts:
2 left
💡 Hint
Snapshot tests only compare how things look, not how they work.
📝 Syntax
expert
2:00remaining
What error does this snapshot test code produce?
What error will this Swift snapshot test code produce when run?
iOS Swift
func testViewSnapshot() {
  let view = UIView()
  assertSnapshot(matching: view, as: .image)
}
ARuntime error because the view has no size and cannot render an image.
BCompile error because assertSnapshot is undefined.
CSyntax error due to missing import statement for snapshot library.
DNo error; the test passes successfully.
Attempts:
2 left
💡 Hint
Empty views without size cannot produce valid snapshots.