Challenge - 5 Problems
Unit Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter unit test?
Consider this simple Dart function and its unit test. What will the test output be when run?
Flutter
int add(int a, int b) { return a + b; } void main() { test('add returns correct sum', () { expect(add(2, 3), 5); }); }
Attempts:
2 left
💡 Hint
Check what the add function returns and what the expect statement checks.
✗ Incorrect
The add function correctly returns the sum of two integers. The test expects add(2, 3) to be 5, which matches the function output, so the test passes.
📝 Syntax
intermediate2:00remaining
Which option contains a syntax error in this Flutter unit test?
Identify the option that will cause a syntax error when running this Flutter unit test code.
Flutter
void main() { test('string length test', () { String text = 'hello'; expect(text.length, 5); }); }
Attempts:
2 left
💡 Hint
Look for missing semicolons or wrong expect usage.
✗ Incorrect
Option A is missing a semicolon after String text = 'hello', causing a syntax error. Other options have different issues but no syntax errors.
❓ lifecycle
advanced2:00remaining
What happens if you forget to call setUp() in a Flutter test suite?
In a Flutter test file, you have a setUp() function that initializes some variables before each test. What is the effect of not calling setUp() explicitly?
Attempts:
2 left
💡 Hint
Think about how the test framework manages setUp functions.
✗ Incorrect
Flutter's test package automatically calls setUp() before each test. You do not call it manually. Forgetting to call it has no effect.
🔧 Debug
advanced2:00remaining
Why does this Flutter unit test fail unexpectedly?
Given this test code, why does the test fail even though the function seems correct?
int multiply(int a, int b) {
return a * b;
}
void main() {
test('multiply test', () {
expect(multiply(2, 3), 5);
});
}
Attempts:
2 left
💡 Hint
Check the expected value against the actual function output.
✗ Incorrect
The multiply function returns 6 for inputs 2 and 3, but the test expects 5, causing the test to fail.
🧠 Conceptual
expert2:00remaining
What is the main benefit of using mocks in Flutter unit testing?
Why do developers use mock objects in Flutter unit tests?
Attempts:
2 left
💡 Hint
Think about how mocks help when your code depends on external services or complex objects.
✗ Incorrect
Mocks simulate dependencies so tests focus only on the unit's logic, making tests reliable and fast.