Challenge - 5 Problems
Swift TDD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of writing tests before code in TDD?
In Test-driven development (TDD), why do developers write tests before writing the actual code?
Attempts:
2 left
💡 Hint
Think about how tests help shape the code you write.
✗ Incorrect
In TDD, writing tests first helps clarify what the code should do, guiding development and ensuring correctness.
❓ ui_behavior
intermediate1:30remaining
What happens when a failing test is run in Xcode during TDD?
You write a test for a new feature in Swift and run it in Xcode, but the feature code is not implemented yet. What will you see?
Attempts:
2 left
💡 Hint
Think about what happens when code does not meet test expectations.
✗ Incorrect
A failing test shows a red mark in Xcode, indicating the feature is not yet implemented or does not behave as expected.
❓ lifecycle
advanced2:00remaining
What is the correct TDD cycle order?
Arrange the steps of the Test-driven development cycle in the correct order.
Attempts:
2 left
💡 Hint
Remember the mantra: Red, Green, Refactor.
✗ Incorrect
TDD follows the cycle: write a failing test (red), write code to pass it (green), then refactor cleanly.
📝 Syntax
advanced1:30remaining
What is the output of this Swift test code snippet?
Consider this Swift test function:
func testSum() {
let result = sum(2, 3)
XCTAssertEqual(result, 6)
}
Assuming sum adds two numbers, what will happen when this test runs?
iOS Swift
func sum(_ a: Int, _ b: Int) -> Int { return a + b }
Attempts:
2 left
💡 Hint
Check the expected value in XCTAssertEqual.
✗ Incorrect
The sum function returns 5, but the test expects 6, so the test fails.
🔧 Debug
expert2:00remaining
Why does this Swift test crash with 'fatal error: unexpectedly found nil while unwrapping an Optional'?
Given this test code:
func testUserName() {
let user: User? = nil
XCTAssertEqual(user!.name, "Alice")
}
Why does this test crash instead of failing?
Attempts:
2 left
💡 Hint
Focus on the exclamation mark after user.
✗ Incorrect
Force-unwrapping a nil Optional causes a runtime crash, not a test failure.