I have a question regarding unit testing in Angular using Jasmin/Karma.
Currently, I am working with three services: EmployeeService, SalaryService, and TaxationService.
The EmployeeService depends on the SalaryService, which is injected into its constructor.
Similarly, the SalaryService depends on the Taxation service, which is also injected into its constructor.
In my employee.component.spec.ts file, the following code snippet can be found:
let employeeService: EmployeeService;
let salaryServiceSpy: jasmine.SpyObj<SalaryService>;
TestBed.configureTestingModule({
providers: [EmployeeService],
declarations: [ EmployeeComponent ]
})
.compileComponents();
employeeService = TestBed.inject(EmployeeService);
salaryServiceSpy = TestBed.inject(SalaryService) as jasmine.SpyObj<SalaryService>;
}));
it('should return Name and Salary', () => {
expect(employeeService.getEmployeeNameAndSalary(1)).toBe("John Smith receives: $12345 and tax value of 15%");
});
While the code seems to be functioning correctly, I'm puzzled about the necessity of using Jasmine Spy. The code runs fine even without implementing the spy service?
salaryServiceSpy = TestBed.inject(SalaryService) as jasmine.SpyObj<SalaryService>;
I came across this example for reference: https://angular.io/guide/testing-services.
If we consider the example below, why do we need to create the ValueServiceSpy? Doesn't TestBed.inject already handle the injection of all dependencies and the 'dependencies of the other dependencies'?