Bird
Raised Fist0
Rubyprogramming~20 mins

Test-driven development workflow in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TDD Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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
ASyntaxError: unexpected end-of-input, expecting keyword_end
B
Failure/Error: expect(calc.add(2, 3)).to eq(5)

RSpec::Expectations::ExpectationNotMetError:
  expected: 5
       got: nil
C
Failure/Error: expect(calc.add(2, 3)).to eq(5)

NoMethodError:
  undefined method `add' for #<Calculator:0x00007f...>
DTest passed successfully with 1 example, 0 failures
Attempts:
2 left
💡 Hint
Think about what happens if you call a method that does not exist yet on an object.
🧠 Conceptual
intermediate
1: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?
ATo ensure the test framework is working and to define the expected behavior clearly before coding
BTo speed up coding by skipping tests and writing code directly
CTo make the code run faster by pre-checking errors
DTo avoid writing documentation for the code
Attempts:
2 left
💡 Hint
Think about how TDD helps clarify requirements and verify correctness.
❓ Predict Output
advanced
2: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
ANoMethodError: undefined method `add' for Calculator
B1 example, 0 failures
CSyntaxError: undefined local variable or method `a' for Calculator
D
1 example, 1 failure
Failure/Error: expect(calc.add(2, 3)).to eq(5)

RSpec::Expectations::ExpectationNotMetError:
  expected: 5
       got: 0
Attempts:
2 left
💡 Hint
Check if the add method returns the sum of two numbers.
🔧 Debug
advanced
2: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
ASyntaxError: unexpected end-of-input, expecting keyword_end
BNoMethodError: undefined method `subtract' for Calculator
C1 example, 0 failures
DRSpec::Expectations::ExpectationNotMetError: expected 2 but got 8
Attempts:
2 left
💡 Hint
Look carefully at the method definition and class structure.
🧠 Conceptual
expert
1: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?
ATo rewrite the tests to match the new code instead of the other way around
BTo add more features immediately without checking existing code
CTo remove all tests since the code works now
DTo improve code quality and maintainability without changing behavior, ensuring tests still pass
Attempts:
2 left
💡 Hint
Think about the purpose of refactoring in clean code practices.

Practice

(1/5)
1. In Ruby's test-driven development (TDD) workflow, what is the first step you should do?
easy
A. Run all tests to check for errors
B. Write a failing test that defines the desired behavior
C. Refactor the existing code
D. Write the full implementation code

Solution

  1. Step 1: Understand the TDD cycle

    The TDD cycle starts by writing a test that fails because the feature is not implemented yet.
  2. Step 2: Identify the first action in TDD

    Writing a failing test first guides the development and ensures the code meets requirements.
  3. Final Answer:

    Write a failing test that defines the desired behavior -> Option B
  4. Quick Check:

    First step in TDD = Write failing test [OK]
Hint: Remember: Test first, code second in TDD [OK]
Common Mistakes:
  • Starting by writing implementation code before tests
  • Refactoring before any test is written
  • Running tests before any code or test exists
2. Which Ruby command runs all tests in a typical TDD workflow using Minitest?
easy
A. rails server
B. rspec test_file.rb
C. ruby test_file.rb
D. bundle install

Solution

  1. Step 1: Identify the test framework command

    Minitest tests are run by executing the Ruby test file directly with ruby test_file.rb.
  2. Step 2: Eliminate unrelated commands

    rspec is for RSpec tests, rails server starts the server, and bundle install installs gems.
  3. Final Answer:

    ruby test_file.rb -> Option C
  4. Quick Check:

    Run Minitest with ruby test_file.rb [OK]
Hint: Run tests by executing test files with ruby [OK]
Common Mistakes:
  • Using rspec command for Minitest tests
  • Confusing server start with test run
  • Running bundle install instead of tests
3. Given this Ruby test and code, what will be the test output?
require 'minitest/autorun'

class Calculator
  def add(a, b)
    a + b
  end
end

class TestCalculator < Minitest::Test
  def test_add
    calc = Calculator.new
    assert_equal 5, calc.add(2, 3)
  end
end
medium
A. 1 test, 0 failures
B. 1 test, 1 failure
C. SyntaxError
D. No tests run

Solution

  1. Step 1: Analyze the test and method

    The add method returns the sum of two numbers. The test expects 5 from adding 2 and 3.
  2. Step 2: Check if test passes

    Since 2 + 3 equals 5, the assertion passes and the test succeeds.
  3. Final Answer:

    1 test, 0 failures -> Option A
  4. Quick Check:

    Correct sum = test passes [OK]
Hint: If method matches test expectation, test passes [OK]
Common Mistakes:
  • Thinking test fails when it actually passes
  • Confusing syntax error with test failure
  • Ignoring the assert_equal expected value
4. What is wrong with this TDD workflow snippet in Ruby?
class TestCalculator < Minitest::Test
  def test_multiply
    calc = Calculator.new
    assert_equal 6, calc.multiply(2, 3)
  end
end

class Calculator
  def multiply(a, b)
    a - b
  end
end
medium
A. The test method name is incorrect
B. assert_equal arguments are reversed
C. Calculator class is missing
D. The multiply method subtracts instead of multiplies

Solution

  1. Step 1: Compare method implementation with test expectation

    The test expects multiply(2, 3) to return 6, but the method subtracts a - b.
  2. Step 2: Identify the error cause

    Subtracting 2 - 3 gives -1, which will fail the test expecting 6.
  3. Final Answer:

    The multiply method subtracts instead of multiplies -> Option D
  4. Quick Check:

    Method logic must match test expectation [OK]
Hint: Check method logic matches test assertions [OK]
Common Mistakes:
  • Confusing subtraction with multiplication
  • Ignoring method implementation details
  • Assuming test method name affects test result
5. You want to safely improve your Ruby code after passing tests. Which TDD step lets you do this without breaking functionality?
hard
A. Refactor the code while running tests frequently
B. Write new tests before changing code
C. Run tests only after all changes are done
D. Delete old tests to simplify code

Solution

  1. Step 1: Understand the role of refactoring in TDD

    Refactoring means improving code structure without changing behavior, done safely with tests as a safety net.
  2. Step 2: Identify the correct practice

    Running tests frequently during refactoring ensures no functionality breaks.
  3. Final Answer:

    Refactor the code while running tests frequently -> Option A
  4. Quick Check:

    Refactor + frequent tests = safe improvements [OK]
Hint: Refactor with tests running often to catch errors [OK]
Common Mistakes:
  • Running tests only after all changes
  • Deleting tests which protect code quality
  • Writing tests after refactoring instead of before
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard