Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baszealot13 opened this issue Dec 10, 2017 · 0 comments

Comments

@baszealot13
Copy link

*****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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant