Challenge - 5 Problems
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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)
}Attempts:
2 left
💡 Hint
Snapshot tests compare the UI's look to a saved image.
✗ Incorrect
Snapshot testing captures the UI component's rendered image and compares it to a stored reference image to detect visual changes.
❓ lifecycle
intermediate2: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?
Attempts:
2 left
💡 Hint
Snapshot tests should reflect intentional UI updates.
✗ Incorrect
When UI changes are intentional, update the stored snapshot to the new correct image to keep tests valid.
🔧 Debug
advanced2: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)
}Attempts:
2 left
💡 Hint
UI components need a size to render properly in snapshot tests.
✗ Incorrect
Without a frame or layout constraints, the label's size is zero, so the snapshot image is empty or mismatched.
🧠 Conceptual
advanced2:00remaining
What is a key limitation of snapshot testing?
Which of these is a main limitation of snapshot testing in mobile apps?
Attempts:
2 left
💡 Hint
Snapshot tests only compare how things look, not how they work.
✗ Incorrect
Snapshot tests only catch visual changes; they do not test app logic or behavior.
📝 Syntax
expert2: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)
}Attempts:
2 left
💡 Hint
Empty views without size cannot produce valid snapshots.
✗ Incorrect
The view has zero size by default, so snapshotting it produces a runtime failure or empty image error.