Below is a method written in the service called CapacitorManager
:
backButtonHandler() {
return () => {
const activeModalInstance = this.dataService.getItem(Constants.activeModalInstanceKey);
const doesActiveModalExist = activeModalInstance !== -1;
if (doesActiveModalExist) {
activeModalInstance.close();
}
else {
this.location.back();
}
}
}
This method should invoke the close
method within the object activeModalInstance
(where activeModalInstance
is an instance of NgbModal) if there is an activeModalInstance
inside the instance referred to by dataService.getItem()
. The getItem
method has already been mocked to return an object including the close method.
fdescribe('CapacitorManager', () => {
let service: CapacitorManager;
let dataService: DataService;
let location: Location;
let dataServiceStub: Partial<DataService>;
beforeEach(async(() => {
dataServiceStub = {
getItem: () => {
return {
close: () => {
}
}
}
}
TestBed.configureTestingModule({
providers: [
{ provide: Location, useClass: SpyLocation },
{ provide: DataService, useValue: dataServiceStub },
CapacitorManager
],
imports: [
RouterTestingModule
]
}).compileComponents().then(() => {
service = TestBed.inject(CapacitorManager);
dataService = TestBed.inject(DataService);
location = TestBed.inject(Location);
});
}));
afterEach(() => {
service = null;
});
it('should create the component', () => {
expect(service).toBeTruthy();
});
it('should backButtonHandler call activeModal in case activeModalInstance exists', () => {
const activeModalInstance = dataService.getItem("activeModal");
spyOn(activeModalInstance, "close");
service.backButtonHandler();
expect(activeModalInstance.close).toHaveBeenCalled();
expect(location.back).not.toHaveBeenCalled();
});
});
The last test case is where I am facing the issue of Expected a spy, but got Function
. How can I pass this test case successfully?