0
0
Rubyprogramming~5 mins

Mocking and stubbing in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Amock(object).method
Ballow(object).to receive(:method).and_return(value)
Cexpect(object).to receive(:method)
Dstub(object).method
What does a mock object do besides stubbing methods?
AVerifies that methods are called with expected arguments
BRuns the real method implementation
CDeletes the original object
DAutomatically fixes bugs
Why is stubbing useful in tests?
ATo replace slow or unreliable methods with fixed return values
BTo make tests run slower
CTo delete code
DTo create real database records
Which of these is NOT a benefit of mocking and stubbing?
AIsolating the code under test
BControlling test environment
CSpeeding up tests
DMaking tests dependent on external services
In RSpec, how do you check that a method was called on a mock?
Aobject.method
Ballow(object).to receive(:method)
Cexpect(object).to receive(:method)
Dstub(object).method
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.