Bird
Raised Fist0
Rubyprogramming~20 mins

Minitest basics (assert style) in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Minitest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Minitest assertion?
Consider the following Minitest code snippet. What will be the result when this test runs?
Ruby
require 'minitest/autorun'

class TestExample < Minitest::Test
  def test_sum
    sum = 2 + 3
    assert_equal(5, sum)
  end
end
ATest passes with no errors
BTest fails with an assertion error
CSyntaxError due to missing require
DRuntimeError due to undefined method
Attempts:
2 left
💡 Hint
Check what assert_equal expects and what sum is.
❓ Predict Output
intermediate
2:00remaining
What error does this Minitest assertion raise?
What error will this test produce when run?
Ruby
require 'minitest/autorun'

class TestExample < Minitest::Test
  def test_truth
    assert(false, "This should fail")
  end
end
ANameError for undefined assert
BNo error, test passes
CSyntaxError due to assert usage
DMinitest::Assertion with message 'This should fail'
Attempts:
2 left
💡 Hint
assert expects a true value to pass.
🔧 Debug
advanced
2:30remaining
Why does this Minitest assertion fail unexpectedly?
Examine the test below. Why does the assertion fail even though the values look equal?
Ruby
require 'minitest/autorun'

class TestExample < Minitest::Test
  def test_array_equality
    a = [1, 2, 3]
    b = [1, 2, 3.0]
    assert_equal(a, b)
  end
end
ABecause 3 (integer) is not equal to 3.0 (float)
BBecause arrays cannot be compared with assert_equal
CBecause assert_equal requires strings, not arrays
DBecause the test is missing a require statement
Attempts:
2 left
💡 Hint
Check the types of elements inside the arrays.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this Minitest test?
Identify the option that will cause a syntax error when running this test code.
Ruby
require 'minitest/autorun'

class TestExample < Minitest::Test
  def test_truth
    assert true
  end
end
A
def test_truth
  assert true
end
B
def test_truth
  assert true
C
def test_truth
  assert(true)
end
D
dne
)eurt(tressa  
hturt_tset fed
Attempts:
2 left
💡 Hint
Check if all method definitions are properly closed.
🚀 Application
expert
2:30remaining
How many assertions are run in this Minitest test method?
Given the following test method, how many assertions will Minitest count when this test runs?
Ruby
require 'minitest/autorun'

class TestExample < Minitest::Test
  def test_multiple_asserts
    assert_equal(4, 2 + 2)
    assert(3 > 1)
    assert_nil(nil)
  end
end
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count each assert or assert_equal call as one assertion.

Practice

(1/5)
1. What does the assert method in Minitest do?
easy
A. Checks if a condition is true and fails the test if not
B. Runs the entire test suite automatically
C. Prints the test results to the console
D. Defines a new test case class

Solution

  1. Step 1: Understand the purpose of assert

    The assert method is used to verify that a condition is true during a test.
  2. Step 2: What happens if the condition is false?

    If the condition is false, the test fails and reports an error.
  3. Final Answer:

    Checks if a condition is true and fails the test if not -> Option A
  4. Quick Check:

    assert checks truth = B [OK]
Hint: Assert checks if condition is true, else test fails [OK]
Common Mistakes:
  • Thinking assert runs all tests
  • Confusing assert with printing output
  • Believing assert defines test classes
2. Which of the following is the correct syntax to write a test method in Minitest using assert style?
easy
A. function test_example() { assert_equal(5, 2 + 3); }
B. def test_example; assert_equal 5, 2 + 3; end
C. test 'example' do; assert_equal 5, 2 + 3; end
D. test_example() { assert_equal 5, 2 + 3 }

Solution

  1. Step 1: Identify Ruby method syntax for Minitest

    In Minitest, test methods start with def test_... and end with end.
  2. Step 2: Check the assert usage inside the method

    assert_equal expected, actual is used correctly inside the method.
  3. Final Answer:

    def test_example; assert_equal 5, 2 + 3; end -> Option B
  4. Quick Check:

    Ruby test method syntax = C [OK]
Hint: Test methods start with def test_ and end with end [OK]
Common Mistakes:
  • Using non-Ruby syntax like function or braces
  • Using test do blocks which belong to other frameworks
  • Missing def or end keywords
3. What will be the output when running this Minitest code?
require 'minitest/autorun'

class SampleTest < Minitest::Test
  def test_sum
    assert_equal 10, 5 + 5
  end

  def test_fail
    assert 3 > 5, '3 is not greater than 5'
  end
end
medium
A. One test passes, one test fails with message '3 is not greater than 5'
B. Both tests pass successfully
C. Syntax error due to incorrect assert usage
D. Both tests fail without any message

Solution

  1. Step 1: Analyze test_sum method

    assert_equal 10, 5 + 5 checks if 10 equals 10, which is true, so this test passes.
  2. Step 2: Analyze test_fail method

    assert 3 > 5 checks if 3 is greater than 5, which is false, so this test fails and shows the message '3 is not greater than 5'.
  3. Final Answer:

    One test passes, one test fails with message '3 is not greater than 5' -> Option A
  4. Quick Check:

    assert_equal true, assert false with message = D [OK]
Hint: Check each assert condition true or false to predict pass/fail [OK]
Common Mistakes:
  • Assuming both tests pass
  • Confusing assert and assert_equal behavior
  • Ignoring failure message in assert
4. Identify the error in this Minitest code snippet:
class MyTest < Minitest::Test
  def test_check
    assert_equal(4, 2 + 2
  end
end
medium
A. Test method name does not start with 'test_'
B. Incorrect class inheritance from Minitest::Test
C. Missing closing parenthesis in assert_equal call
D. assert_equal arguments are in wrong order

Solution

  1. Step 1: Check syntax of assert_equal call

    The line assert_equal(4, 2 + 2 is missing a closing parenthesis.
  2. Step 2: Verify other parts of the code

    Class inheritance and method name are correct, and argument order is correct (expected, actual).
  3. Final Answer:

    Missing closing parenthesis in assert_equal call -> Option C
  4. Quick Check:

    Syntax errors often from missing parentheses = A [OK]
Hint: Count parentheses carefully in assert calls [OK]
Common Mistakes:
  • Ignoring missing parentheses causing syntax errors
  • Thinking test method name can be arbitrary
  • Confusing argument order in assert_equal
5. You want to write a Minitest that checks if a method greet(name) returns 'Hello, name!'. Which assert statement correctly tests this behavior?
hard
A. assert greet('Alice') = 'Hello, Alice!'
B. assert 'Hello, Alice!', greet('Alice')
C. assert_equal greet('Alice'), 'Hello Alice!'
D. assert_equal 'Hello, Alice!', greet('Alice')

Solution

  1. Step 1: Understand assert_equal argument order

    The first argument is the expected value, the second is the actual result from the method.
  2. Step 2: Check each option's argument order and syntax

    assert_equal 'Hello, Alice!', greet('Alice') correctly uses expected first, actual second. assert 'Hello, Alice!', greet('Alice') asserts a truthy string (always passes), ignoring actual result. assert_equal greet('Alice'), 'Hello Alice!' reverses order and uses wrong expected (missing comma). assert greet('Alice') = 'Hello, Alice!' uses assignment (=), syntax error.
  3. Final Answer:

    assert_equal 'Hello, Alice!', greet('Alice') -> Option D
  4. Quick Check:

    Expected first, actual second in assert_equal = A [OK]
Hint: assert_equal expects expected value first, actual second [OK]
Common Mistakes:
  • Swapping expected and actual arguments
  • Using assignment (=) instead of comparison (==)
  • Using assert with assignment causing syntax error
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