011. The Power of Spies
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
A 'Spy' is a double agent. It replaces a real dependency with a controlled version that you can monitor. Using jasmine.createSpyObj(), you can verify not only that a method was called, but how many times it was called and with what exact arguments. This allows you to test that your component is correctly interacting with its services without actually triggering the side effects of those services, such as saving to a database or navigating the router.
022. Mocking the Network
You should never make real API calls in a unit test. Real networks are slow and unpredictable. Angular's HttpClientTestingModule provides a 'mock' backend. You can use the HttpTestingController to expect a specific URL, verify the request method (GET, POST), and then 'flush' it with a mock JSON response. This makes your tests synchronous and extremely fast, ensuring that you can verify your data-handling logic in milliseconds.
?Frequently Asked Questions
What is Angular?
Angular is a platform and framework built by Google for building single-page client applications using HTML and TypeScript.
What is a Component in Angular?
In Angular, a Component is the basic building block of the UI. Each component consists of an HTML template, a TypeScript class for logic, and a CSS styles file.
What is dependency injection in Angular?
Dependency Injection (DI) is a core design pattern in Angular where classes request dependencies (like data services) from external sources rather than creating them directly.
