I have a query regarding Angular unit testing using Jasmin/Karma.
I have 3 services as below: EmployeeService, SalaryService and TaxationService.
EmployeeService depends on SalaryService which is injected in it's constructor.
SalaryService depends on Taxation service which is again injected in it's constructor.
In my employee.component.spec.ts, I have the below code:
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%");
});
The code works fine, my question is why do we need to use Jasmine Spy - the code works fine without the spy service as well?
salaryServiceSpy = TestBed.inject(SalaryService) as jasmine.SpyObj<SalaryService>;
I have used the below as reference: https://angular.io/guide/testing-services.
If below example is more clear, what is the need to create the ValueServiceSpy? Does the TestBed.inject not already inject all dependencies and the 'dependencies of the other dependencies'?
