ALGDB
ALGDB

Reputation: 680

NullInjectorError: StaticInjectorError[[object Object]]

I'm trying to create my first unit-test using Jasmine and Karma.

And I cannot figure out why I got that error.

following the code:

AppComponent

import { Service1 } from './_services';
import { Service2 } from './_services';



  // ...Other import

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: [],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements OnInit, AfterViewInit {
  public someObs1$ = this.service1.someObs1$;
  public someObs2$ = this.service2.someObs1$;

  private me = 'app';

  constructor(
    private service1: Service1,
    private service2: Service2,

  ) {
  }

  ngOnInit(): void {
    this.service1.someMethod1(this.me);

  }

  ngAfterViewInit(): void {
   this.someObs1$.pipe(filter((val)=> val).subscribe(()=> this.service2.someMethod2(this.me));

  }
}

and here the Test I created

app.component.spec.ts

import {
  TestBed,
  async,
  ComponentFixture,
  inject,
  tick,
  fakeAsync,
} from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

import { Service1 } from './_services';
import { Service2 } from './_services';
import { DebugElement, Component, Input } from '@angular/core';

import { from, of } from 'rxjs';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-toolbar',
  template: '',
})
export class ToolbarStubComponent {
  @Input() isXs = false;
  @Input() isMobile = false;
}

@Component({
  selector: 'app-notifications',
  template: '',
})
export class NotificationsStubComponent {}

describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let de: DebugElement;

  let service1Spy: jasmine.SpyObj<Service1>;
  let service2Spy: jasmine.SpyObj<Service2>;

    const spy1 = jasmine.createSpyObj('Service1', ['someMethod1']);
    const spy2 = jasmine.createSpyObj('Service2', ['someMethod2']);


    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        NotificationsStubComponent,
        ToolbarStubComponent,
      ],
      imports: [CommonModule, RouterTestingModule.withRoutes([])],
      providers: [
        { provide: Service1, useValue: spy1 },
        { provide: Service2, useValue: spy2 },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    de = fixture.debugElement;

    service1Spy = TestBed.get(spy1); // <-- Error here!
    service2Spy = TestBed.get(spy2);
    // fixture.detectChanges();
    expect(component).toBeDefined();
  });

});

The error seem to be at the line where I called TestBed.get()

the error

NullInjectorError: StaticInjectorError[[object Object]]: 
  NullInjectorError: No provider for [object Object]!
    at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:725:1)
    at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11917:1)
    at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11861:1)
    at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11763:1)
    at TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/testing.js:2448:1)
    at Function.TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm5/testing.js:2229:1)
    at Object.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:99:17)
    at step (http://localhost:9876/_karma_webpack_/main.js:5911:23)
    at Object.next (http://localhost:9876/_karma_webpack_/main.js:5892:53)
    at http://localhost:9876/_karma_webpack_/main.js:5886:71

is there any silly error on the TestBed or Spy configuration?

Upvotes: 2

Views: 2698

Answers (1)

user4676340
user4676340

Reputation:

The injector uses a WeakMap to fetch the dependencies.

This means it checks the type of the object, not the memory reference.

Try this

service1Spy = TestBed.get(Service1);

Upvotes: 2

Related Questions