Mocking Dependency Injection Objects
There may be times where the service would be injected into the component using constructor by DI. In such a case while creating a component instance for test class we could end up with a problem. To answer this we pass mock objects as alternate to constructors
add.employee.component.ts
export class EmployeeComponent implements OnInit { . . constructor(private objEmpserviceService : EmpserviceService) { } checkForAuthentication(): string { if(this.objEmpserviceService.authenticateEmp('Admin')){ return "You are allowed to Edit"; } return "You are Not allowed to Edit"; } . .
Two Methods to Answer this Problem
Method 1 : If you are not going to call the mockedObject methods
To address this the injected object using dependency injection we use providers. Providers provide mockedobject to constructor instead of real object like below
add.employee.component.spec.ts
let mockEmpserviceService:EmpserviceService; beforeEach(async(() => { mockEmpserviceService = jasmine.createSpyObj(['authenticateEmp', 'addEmployee', 'deleteEmployee']); TestBed.configureTestingModule({ . declarations: [ EmployeeComponent ], providers : [ {provide:EmpserviceService, useValue:mockEmpserviceService} ] . . }).compileComponents(); }));
. it('Check for Service Method', ()=>{ spyOn(component, 'checkForAuthentication'); let auth = component.checkForAuthentication(); (or) const btnAddEmp = fixture.debugElement.query(By.css("#btnAddEmp")); btnAddEmp.triggerEventHandler('click', {}); fixture.detectChanges(); expect(auth).toHaveCalled(); }); .
Method 2 : If you are going to call the mocked object methods then in such case the return type of the method should be defined.The same code can be done using actual EmpserviceService in provider and using spyOn inside its block to define the returnType of service method.
let empService: EmpserviceService; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ EmployeeComponent], providers : [ EmpserviceService], imports: [HttpClientTestingModule] }) .compileComponents(); empService = Testbed.inject(EmpserviceService); })); fit('Check for Service Method', ()=>{ //Mocking authenticateEmp to return true as if it is called spyOn(empService, 'authenticateEmp').withArgs('Admin').and.returnValue(true); spyOn(component, 'checkForAuthentication').and.callThrough(); let auth = component.checkForAuthentication(); expect(auth).toBe('You are allowed to Edit'); })
In the above code, checkForAuthentication calls authenticateEmp in empService since it does callThrough.
Note:Even now the control doesnt goes through empService. It would be called and returnvalue would be sent back.