0
0
Laravelframework~5 mins

Mocking and faking in Laravel - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is mocking in Laravel testing?
Mocking is creating a fake version of a class or interface to simulate its behavior during tests without using the real implementation.
Click to reveal answer
intermediate
What is the difference between mocking and faking in Laravel?
Mocking simulates specific methods and their responses, while faking replaces entire services or classes with simple implementations for testing.
Click to reveal answer
beginner
How do you create a mock of a class in Laravel?
Use the `mock()` helper or `Mockery::mock()` to create a mock object that you can set expectations on for method calls.
Click to reveal answer
beginner
What is a Laravel fake and when would you use it?
A fake is a simple replacement for a service, like the Mail or Queue, used to test if actions were performed without sending real emails or jobs.
Click to reveal answer
beginner
Why is mocking useful in unit testing?
Mocking isolates the code being tested by replacing dependencies, so tests run faster and focus only on the code's logic.
Click to reveal answer
Which Laravel helper is commonly used to create a mock object?
Afake()
Bstub()
Csimulate()
Dmock()
What does a Laravel fake typically replace in tests?
AMiddleware
BEntire services like Mail or Queue
CRoutes
DDatabase connections
Why should you use mocking in unit tests?
ATo isolate the code and avoid real dependencies
BTo slow down tests
CTo test the database
DTo deploy the app
Which package does Laravel use internally for mocking?
AMockery
BPHPUnit Mock
CFaker
DGuzzle
What is the main benefit of faking a service in Laravel tests?
AChange the database schema
BMake the app slower
CAvoid performing real actions like sending emails
DAdd new routes
Explain how mocking helps in Laravel unit testing and give an example of when to use it.
Think about testing a class that calls an external API.
You got /3 concepts.
    Describe the difference between mocking and faking in Laravel and why each is useful.
    Consider testing mail sending vs. testing a method call.
    You got /3 concepts.