I'm trying to create unit tests for my Angular 4/5 components where I have a lot of imports. But after import LocationStrategy I got a:
TypeError: this._platformStrategy.getBaseHref is not a function
That's displaying when I import LocationStrategy from @angular/common.
There is my spec.ts file:
student.component.spec.ts
import {TestBed, ComponentFixture, async} from '@angular/core/testing';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {StudentComponent} from './student.component';
import {StudentService} from './student.service';
import {Student} from './student';
import {RecordsCount} from '../shared/entities/recordsCount';
import {Group} from '../groups/group';
import {GroupsService} from '../groups/groups.service';
import {NO_ERRORS_SCHEMA} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpHandler } from '@angular/common/http';
import { OverlayModule } from '@angular/cdk/overlay';
import { MainMaterialModule } from '../main-material.module';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { UpdateDeleteEntityService } from '../entity-table/update-delete-entity.service';
import { InfoModalService } from '../info-modal/info-modal.service';
import { ActivatedRoute } from '@angular/router';
import { Location, LocationStrategy } from '@angular/common';
describe('StudentComponent', () => {
  let fixture: ComponentFixture<StudentComponent>;
  let component: StudentComponent;
  let mockRouter = {
    navigate: jasmine.createSpy('navigate')
  };
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [StudentComponent],
      schemas: [NO_ERRORS_SCHEMA],
      imports: [MainMaterialModule, HttpClientTestingModule],
      providers: [
        StudentService,
        GroupsService,
        InfoModalService,
        UpdateDeleteEntityService,
        Location,
        LocationStrategy,
        { provide: ActivatedRoute, useValue: mockRouter}]
    }).compileComponents();
  }));
  beforeEach(() => {
    fixture = TestBed.createComponent(StudentComponent);
    component = fixture.debugElement.componentInstance;
    fixture.detectChanges();
  });
  it('should return array of students', () => {
    const studentService = fixture.debugElement.injector.get(StudentService);
    fixture.detectChanges();
    const students: Student[] = [
      {
        userId: 20,
        gradebookId: 'UX-3311221',
        studentSurname: 'Лящовський',
        studentName: 'Андрій',
        studentFname: 'Іванович',
        groupId: '2',
        plainPassword: '',
        photo: ''
      }, {
        userId: 13,
        gradebookId: 'UY-3019273',
        studentSurname: 'Заник',
        studentName: 'Іван',
        studentFname: 'Григорович',
        groupId: '5',
        plainPassword: '',
        photo: ''
      }, {
        userId: 155,
        gradebookId: 'UT-1029384',
        studentSurname: 'Лінкольн',
        studentName: 'Абрагім',
        studentFname: 'Зимонсович',
        groupId: '1',
        plainPassword: '',
        photo: ''
    }];
    const recordsCount: RecordsCount = {
      numberOfRecords: '3'
    };
    const spy = spyOn(studentService, 'getStudentsRange').and.returnValue(Observable.of([students, recordsCount]));
    component.getStudents();
    expect(component.students).toEqual(students);
  });
});
                You may have found the solution to this already but just in case, and for the benefit of anyone else with the same problem. I encountered this issue and found the following solution worked:
I think the problem is that LocationStrategy is an abstract class and you need to provide a concrete class which extends LocationStrategy such as PathLocationStrategy. I added the following to my list of providers
{ provide: LocationStrategy, useClass: PathLocationStrategy },
and then because PathLocationStrategy has a dependency on APP_BASE_REF I also added a provider for that
{ provide: APP_BASE_HREF, useValue: '/my/app'}
APP_BASE_HREF and PathLocationStrategy are imported from @angular/common the same as Location and LocationStrategy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With