Challenge - 5 Problems
TDD Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a failing test in TDD
What will be the output when running this RSpec test before implementing the method?
Ruby
RSpec.describe Calculator do describe '#add' do it 'adds two numbers' do calc = Calculator.new expect(calc.add(2, 3)).to eq(5) end end end class Calculator # add method not implemented yet end
Attempts:
2 left
💡 Hint
Think about what happens if you call a method that does not exist yet on an object.
✗ Incorrect
Since the add method is not defined in Calculator, Ruby raises a NoMethodError when the test tries to call it. This causes the test to fail with that error.
🧠 Conceptual
intermediate1:30remaining
Purpose of writing a failing test first in TDD
Why do developers write a failing test before implementing the actual code in Test-driven development?
Attempts:
2 left
💡 Hint
Think about how TDD helps clarify requirements and verify correctness.
✗ Incorrect
Writing a failing test first helps clarify what the code should do and confirms that the test setup works. It guides the implementation and ensures tests actually check the intended behavior.
❓ Predict Output
advanced2:00remaining
Result of running a test after implementing minimal code
Given this test and implementation, what is the test output?
Ruby
RSpec.describe Calculator do describe '#add' do it 'adds two numbers' do calc = Calculator.new expect(calc.add(2, 3)).to eq(5) end end end class Calculator def add(a, b) a + b end end
Attempts:
2 left
💡 Hint
Check if the add method returns the sum of two numbers.
✗ Incorrect
The add method correctly returns the sum of a and b, so the test passes with no failures.
🔧 Debug
advanced2:00remaining
Identify the error in this TDD cycle code snippet
What error will this test produce when run?
Ruby
RSpec.describe Calculator do describe '#subtract' do it 'subtracts two numbers' do calc = Calculator.new expect(calc.subtract(5, 3)).to eq(2) end end end class Calculator def subtract(a, b) a - b # missing end here end
Attempts:
2 left
💡 Hint
Look carefully at the method definition and class structure.
✗ Incorrect
The subtract method is missing an 'end' keyword, causing a syntax error when Ruby tries to parse the code.
🧠 Conceptual
expert1:30remaining
Why refactor after tests pass in TDD?
In Test-driven development, after making a failing test pass, why is refactoring the next important step?
Attempts:
2 left
💡 Hint
Think about the purpose of refactoring in clean code practices.
✗ Incorrect
Refactoring cleans up code structure and readability while keeping behavior the same. Tests ensure no changes break functionality during this process.