Recall & Review
beginner
What is mocking in Ruby testing?
Mocking is creating fake objects that simulate real objects' behavior to test interactions without using the real objects.
Click to reveal answer
beginner
What does stubbing mean in Ruby tests?
Stubbing means replacing a method with a fake one that returns a specific value, so the test does not depend on the real method's behavior.
Click to reveal answer
beginner
How do you create a stub for a method in RSpec?
Use
allow(object).to receive(:method).and_return(value) to make the method return the value during tests.Click to reveal answer
intermediate
What is the difference between a mock and a stub?
A stub provides canned responses to method calls, while a mock also verifies that certain methods were called with expected arguments.
Click to reveal answer
beginner
Why use mocking and stubbing in tests?
They isolate the code being tested by replacing dependencies, making tests faster, more reliable, and focused on specific behavior.
Click to reveal answer
Which RSpec method is used to stub a method?
✗ Incorrect
In RSpec,
allow(object).to receive(:method).and_return(value) is used to stub a method.What does a mock object do besides stubbing methods?
✗ Incorrect
Mocks verify that certain methods are called with expected arguments during tests.
Why is stubbing useful in tests?
✗ Incorrect
Stubbing replaces methods with fixed return values to make tests faster and more reliable.
Which of these is NOT a benefit of mocking and stubbing?
✗ Incorrect
Mocking and stubbing reduce dependency on external services, not increase it.
In RSpec, how do you check that a method was called on a mock?
✗ Incorrect
Use
expect(object).to receive(:method) to verify a method call on a mock.Explain in your own words what mocking and stubbing are and why they are useful in Ruby testing.
Think about how you can replace parts of your code to test only what you want.
You got /4 concepts.
Describe how you would stub a method in RSpec and how you would verify a method call using a mock.
Remember the keywords 'allow' and 'expect' in RSpec.
You got /3 concepts.