mardi 13 mars 2018

Inject Location in Angular Test

I user Location injection in my angular component to redirect the user to another route. It works as intended but when I run the tests I get this message

Error: StaticInjectorError(DynamicTestModule)[CreateGroupComponent -> Location]: StaticInjectorError(Platform: core)[CreateGroupComponent -> Location]: NullInjectorError: No provider for Location!

Here is my Test class

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

import { CreateGroupComponent } from './create-group.component';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { GroupService } from '../core/groupService/group.service';
import { AuthService } from '../core/authService/auth.service';
import { RouterTestingModule } from '@angular/router/testing';

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

  beforeEach(async(() => {

    const mockGroupService: any = {

    };

    const mockAuthService: any = {

    };

    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule],
      declarations: [CreateGroupComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      providers: [{ provide: AuthService, useValue: mockAuthService }, { provide: GroupService, useValue: mockGroupService }]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CreateGroupComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

Aucun commentaire:

Enregistrer un commentaire