phenomnomnominal / angular-2-local-storage

LocalStorageService for Angular 2 with the same API as angular-local-storage
MIT License
93 stars 49 forks source link

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

Open adammendoza opened 6 years ago

adammendoza commented 6 years ago

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)
NivedhaL20 commented 5 years ago

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

riede commented 5 years ago

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

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.

Vishwa17 commented 4 years ago

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.