Bird
0
0

You want to ensure your Ruby code handles empty strings correctly. Which test best applies Ruby's testing culture to check this?

hard📝 Application Q15 of 15
Ruby - Testing with RSpec and Minitest
You want to ensure your Ruby code handles empty strings correctly. Which test best applies Ruby's testing culture to check this?
def reverse_string(str)
  str.reverse
end
Adef test_reverse_empty assert_equal nil, reverse_string('') end
Bdef test_reverse_empty assert_equal false, reverse_string('') end
Cdef test_reverse_empty assert_equal ' ', reverse_string('') end
Ddef test_reverse_empty assert_equal '', reverse_string('') end
Step-by-Step Solution
Solution:
  1. Step 1: Understand function behavior with empty string

    Calling reverse on an empty string returns an empty string.
  2. Step 2: Choose assertion matching expected output

    Test should expect '' (empty string), not nil, space, or false.
  3. Final Answer:

    def test_reverse_empty assert_equal '', reverse_string('') end -> Option D
  4. Quick Check:

    Empty string reversed is empty string [OK]
Quick Trick: Test expected output exactly, empty string reverses to empty string [OK]
Common Mistakes:
  • Expecting nil instead of empty string
  • Using space or false as expected result
  • Not testing edge cases like empty input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes