angular-2-local-storage icon indicating copy to clipboard operation
angular-2-local-storage copied to clipboard

ng test error Error: StaticInjectorError(DynamicTestModule)[AuthenticationService -> LocalStorageService]

Open adammendoza opened this issue 7 years ago • 3 comments

I'm getting this error when running ng test

        Error: StaticInjectorError(DynamicTestModule)[AuthenticationService -> LocalStorageService]:
        error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'AuthenticationService', Function ] })
            at <Jasmine>
            at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get node_modules/@angular/core/fesm5/core.js:979:1)
            at resolveToken node_modules/@angular/core/fesm5/core.js:1232:1)
            at tryResolveToken node_modules/@angular/core/fesm5/core.js:1182:1)
            at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1077:1)
            at resolveToken node_modules/@angular/core/fesm5/core.js:1232:1)
            at tryResolveToken node_modules/@angular/core/fesm5/core.js:1182:1)
            at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1077:1)
            at resolveNgModuleDep node_modules/@angular/core/fesm5/core.js:9238:1)
            at _createClass node_modules/@angular/core/fesm5/core.js:9277:1)

adammendoza avatar May 12 '18 06:05 adammendoza

Even I am getting the same error. Can anyone please tell how to solve this issue??

NivedhaL20 avatar Dec 10 '18 11:12 NivedhaL20

Ok. I'm sure you already have solved the problem but here are some solutions:

  • add the LocalStorageModule to the imports list of your test module configuration
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        // other imports...
        LocalStorageModule.forRoot()
      ],
      // declarations, providers etc...
    })
  }));
  • OR provide a simple mock object that contains all functions that are used in your service (e.g. set and get). When you want to return another value than null in a test case simply use returnValue, returnValues, callFake or callThrough for your Spy.
  let localStorageMock: Partial<LocalStorageService>;

  beforeEach(() => {
    localStorageMock = {
      get: jasmine.createSpy('get').and.returnValue(null)
      set: jasmine.createSpy('set')
    }
  });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: LocalStorageService, useValue: localStorageMock }
      ],
      // declarations, imports etc...
    })
  }));
  • OR create a class, that defines a Mock for the LocalStorageService once for your project. This class has to contain all public functions of the LocalStorageService. Make sure that the class will be excluded when you create the app bundle for production.
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: LocalStorageService, useClass: LocalStorageServiceMock }
      ],
      // declarations, imports etc...
    })
  }));

When you use your AuthenticationService in another service or component make sure that you have created a mock for the LocalStorageService as well because you have a dependency in your service to the LocalStorageService. Another way is to create a mock class for your AuthenticationService that has no dependencies to other services.

riede avatar Mar 01 '19 14:03 riede

It is very simple, like if you are using the service in HomePage, so go to HomePage -> home.page.spec.ts then

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { HomePage } from './home.page';
import { NativeStorage } from '@ionic-native/native-storage/ngx';

describe('HomePage', () => {
  let component: HomePage;
  let fixture: ComponentFixture<HomePage>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ HomePage ],
      imports: [IonicModule.forRoot()],
      providers: [LocalStorageService]
    }).compileComponents();

    fixture = TestBed.createComponent(HomePage);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Just add providers , add provider name and your test will run.

Vishwa17 avatar Jan 30 '20 09:01 Vishwa17