MCQs on Testing and Debugging | Ruby

Gain a deeper understanding of testing and debugging in Ruby, focusing on unit testing frameworks like Minitest and RSpec, as well as essential debugging tools such as byebug and pry. These 30 multiple-choice questions will guide you through essential testing concepts and debugging practices.


Introduction to Unit Testing with Minitest or RSpec

  1. Which of the following is a popular testing framework used in Ruby?
    • A) Minitest
    • B) RSpec
    • C) Both A and B
    • D) None of the above
  2. Which gem is required to use RSpec for testing in Ruby?
    • A) rspec-core
    • B) minitest
    • C) rspec
    • D) test-unit
  3. What is the default assertion method used in Minitest?
    • A) assert_equal
    • B) assert_true
    • C) assert_instance_of
    • D) assert_match
  4. How do you run RSpec tests from the command line?
    • A) ruby rspec_tests.rb
    • B) rspec
    • C) rspec test
    • D) test rspec
  5. What method in RSpec is used to define a test example?
    • A) it
    • B) test
    • C) describe
    • D) example
  6. In Minitest, which method is used to check equality between two values?
    • A) assert_equal
    • B) assert_match
    • C) assert_same
    • D) assert_raises
  7. Which RSpec method is used to group tests together?
    • A) describe
    • B) group
    • C) it
    • D) test
  8. What is the purpose of before and after hooks in RSpec?
    • A) To execute code before and after each test
    • B) To define the test cases
    • C) To ignore failed tests
    • D) To skip the current test
  9. How do you define a test case in RSpec?
    • A) test “should work” do
    • B) it “should work” do
    • C) define test_case “should work”
    • D) test_case do
  10. In Minitest, which method is used to check if an error is raised?
    • A) assert_raises
    • B) assert_error
    • C) assert_thrown
    • D) assert_fail
  11. Which of the following is true about RSpec and Minitest?
    • A) Both are used for unit testing
    • B) RSpec is more declarative, while Minitest is more imperative
    • C) Minitest is faster than RSpec
    • D) All of the above
  12. Which method in RSpec is used to expect a value to raise an exception?
    • A) expect { }.to raise_error
    • B) should_raise
    • C) expect_to_raise
    • D) expect_error
  13. Which testing framework is included by default in Ruby?
    • A) RSpec
    • B) Minitest
    • C) Test::Unit
    • D) Cucumber
  14. What is the context keyword used for in RSpec?
    • A) To group related examples based on a common condition
    • B) To define a test case
    • C) To describe the object being tested
    • D) To list all the assertions
  15. How do you specify an expectation in RSpec?
    • A) expect(value).to eq(expected_value)
    • B) assert(value).eq(expected_value)
    • C) assert_equal(value, expected_value)
    • D) should_equal(value, expected_value)
  16. What is the purpose of mocking and stubbing in testing?
    • A) To simulate the behavior of objects or methods in tests
    • B) To check if the code is working
    • C) To measure the code’s performance
    • D) To skip tests that are not relevant
  17. How do you run a single test case in RSpec?
    • A) rspec test_spec.rb
    • B) rspec -t test_case
    • C) ruby test_spec.rb
    • D) rspec –run test_case
  18. Which command will generate an RSpec test file?
    • A) rspec generate
    • B) rspec –init
    • C) rspec new
    • D) rspec –new-file
  19. What is the let keyword used for in RSpec?
    • A) To define variables for test examples
    • B) To define a helper method
    • C) To group related tests
    • D) To define a class method
  20. Which of the following is a valid RSpec matcher for testing whether a string includes a substring?
    • A) include
    • B) match
    • C) contain
    • D) has

Debugging Tools (byebug, pry)

  1. Which gem is used for debugging Ruby code with breakpoints?
    • A) pry
    • B) byebug
    • C) debugger
    • D) both A and B
  2. How do you add a breakpoint in Ruby using byebug?
    • A) byebug
    • B) breakpoint
    • C) debug
    • D) stop_debug
  3. Which command is used in pry to step through the code line by line?
    • A) step
    • B) next
    • C) continue
    • D) trace
  4. What is the purpose of binding.pry in Ruby?
    • A) It pauses the execution of code and opens an interactive session
    • B) It prints the value of a variable
    • C) It skips the current line of code
    • D) It checks for syntax errors
  5. How do you exit a pry session?
    • A) exit
    • B) quit
    • C) Ctrl + D
    • D) exit_session
  6. Which method in byebug allows you to step through code after a breakpoint?
    • A) step
    • B) next
    • C) continue
    • D) next_line
  7. In byebug, what command is used to continue execution after hitting a breakpoint?
    • A) step
    • B) next
    • C) continue
    • D) run
  8. Which of the following can be used to inspect variables in pry?
    • A) pry variable_name
    • B) show variable_name
    • C) display variable_name
    • D) var variable_name
  9. Which command in byebug shows the current stack trace?
    • A) stack
    • B) where
    • C) trace
    • D) call
  10. Which of the following is NOT a feature of the pry gem?
    • A) Interactive REPL
    • B) Syntax highlighting
    • C) Code coverage reports
    • D) Debugging

Answer Key

QnoAnswer (Option with the text)
1C) Both A and B
2C) rspec
3A) assert_equal
4B) rspec
5A) it
6A) assert_equal
7A) describe
8A) To execute code before and after each test
9B) it “should work” do
10A) assert_raises
11D) All of the above
12A) expect { }.to raise_error
13B) Minitest
14A) To group related examples based on a common condition
15A) expect(value).to eq(expected_value)
16A) To simulate the behavior of objects or methods in tests
17A) rspec test_spec.rb
18B) rspec –init
19A) To define variables for test examples
20A) include
21D) both A and B
22A) byebug
23B) next
24A) It pauses the execution of code and opens an interactive session
25C) Ctrl + D
26B) next
27C) continue
28A) pry variable_name
29B) where
30C) Code coverage reports

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top