Integration Testing & Best Practices
Integration tests verify that components, services, and templates work together. They catch real bugs that unit tests miss.
Unit tests test ONE thing in isolation. Integration tests test MULTIPLE things together.
"Unit = ek player ki practice, Integration = team match."
- Unit tests: fast, targeted, 70% of your tests
- Integration tests: catch connection bugs, 20% of tests
- E2E tests: full user flow in browser, 10% of tests
Integration catches: wrong service method, bad data mapping, broken bindings, missing @Inputs.
Integration test: REAL service (not mocked) with mocked HTTP.
// REAL service, fake HTTP
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
BiryaniService, // REAL service!
{ provide: Router, useValue: routerSpy }
]
});"Real service + fake HTTP = integration test."
Catches: wrong method call, wrong data mapping, wrong template binding, missing error handling.
Mock Router to test navigation:
let routerSpy = jasmine.createSpyObj("Router", ["navigate"]);
it("should go to menu", () => {
component.goToMenu();
expect(routerSpy.navigate)
.toHaveBeenCalledWith(["/menu"]);
});"Router mock karo โ navigate call hua ya nahi check karo."
Form testing: Set values, validate, submit.
it("should validate required fields", () => {
component.form.patchValue({
name: "Hyderabadi", price: 500
});
expect(component.form.valid).toBeTrue();
component.form.get("name")?.setValue("");
expect(component.form.valid).toBeFalse();
});"Form fill karo, validate check karo, submit karo, service call check karo."
Best Practices Summary:
- Arrange-Act-Assert โ Setup, call, check. Every test follows this.
- One assertion per test (or closely related group)
- Descriptive names โ "should show error when name is empty"
- Test edge cases โ empty array, null, long strings
- Do not test Angular โ test YOUR logic, not Angular internals
- 80% coverage goal โ focus on integration tests after 80%
"Good tests = documentation โ padhne se samajh aaye component kya karta hai."
Key Takeaways
- Unit tests isolate one piece; Integration tests test connections
- Integration: real service + fake HTTP + mock Router
- Mock Router with jasmine.createSpyObj and toHaveBeenCalledWith
- Test forms by patching values and triggering validation
- Follow Arrange-Act-Assert; aim for 80% coverage
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login