tixif-ni/ngx-localforage

I got TypeError: this.localForage.setItem is not a function when run unit test

baszealot13 opened this issue · 0 comments

*****file *.spec.ts
import { TestBed, inject } from '@angular/core/testing';

import { LocalStorageService } from './local-storage.service';
import { LocalForageService, LocalForageToken } from 'ngx-localforage';

describe('LocalStorageService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [LocalStorageService, LocalForageService, LocalForageToken]
});
});

it('should be created', inject([LocalStorageService], (local: LocalStorageService) => {
    expect(local).toBeTruthy();
}));

it('should have properties and methods', inject([LocalStorageService], (local: LocalStorageService) => {
    expect(local.setAdminAuth).toBeDefined();
    expect(local.getAdminAuth).toBeDefined();
}));

describe('Service return correct result', () => {
    let expectedResult = { username: 'user1', password: 'test1234' };
    it('should pass the result as Promise', inject([LocalStorageService], (local: LocalStorageService) => {
        **local.setAdminAuth**('user1', 'test1234').then((result) => {
            expect(result).toEqual(expectedResult);
        })
    }));
});

});
##############################################################
import { Injectable } from '@angular/core';
import { LocalForageService } from 'ngx-localforage';

@Injectable()
export class LocalStorageService {

constructor(private local: LocalForageService) { }

setAdminAuth(username = '', password = '') {
    return this.local.setItem('admin-auth', { username: username, password: password }).toPromise();
}

getAdminAuth() {
    return this.local.getItem('admin-auth').toPromise();
}

}

######################################################
TypeError: this.localForage.setItem is not a function
at LocalForageService.webpackJsonp.../../../../ngx-localforage/dist/localforage.service.js.LocalForageService.setItem (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/ngx-localforage/dist/localforage.service.js:19:1)
at LocalStorageService.webpackJsonp.../../../../../src/app/local-storage.service.ts.LocalStorageService.setAdminAuth (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/src/app/local-storage.service.ts:10:27)
at UserContext. (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/src/app/local-storage.service.spec.ts:39:19)
at TestBed.webpackJsonp.../../../core/esm5/testing.js.TestBed.execute (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/@angular/core/esm5/testing.js:1013:1)
at UserContext. (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/@angular/core/esm5/testing.js:1143:29)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/zone.js/dist/zone.js:392:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/zone.js/dist/proxy.js:79:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/zone.js/dist/zone.js:391:1)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/zone.js/dist/zone.js:142:1)
at UserContext. (http://localhost:9876/_karma_webpack_/webpack:/usr/local/www/tuifree-web/node_modules/zone.js/dist/jasmine-patch.js:104:1)

How to fix this case?

angular5
test with karma unit test.