Mocking
- Mocking is like making a fake version of something in your code, like a function or an object.
- You create this fake so you can check if it’s used the right way in your tests, but without actually doing the real work.
Example: You make a fake function that pretends to return 10 every time you call it, so you can test how the rest of the code reacts without doing any real calculations.
Patching
- Patching is like putting a temporary sticker over a real part of your code.
- When your code tries to use the real part (like calling a function), it uses the “sticker” (the fake) instead.
- Useful when you don’t want to actually call something complicated, like an API, during testing.
Example: You put a “sticker” over a function in another file, so when your code tries to call it, it just uses the fake version that returns 10 instead of actually calling the real function.
In short:
- Mocking = create a fake version you can play with.
- Patching = temporarily cover up a real thing with a fake one during the test.