Angular TypeError:此.\u platformStrategy.getBaseHref不是函数
我正在尝试为我的Angular TypeError:此.\u platformStrategy.getBaseHref不是函数,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我正在尝试为我的Angular 4/5组件创建单元测试,其中我有很多导入。但在进口定位策略之后,我得到了: TypeError:此.\u platformStrategy.getBaseHref不是函数 当我从@angular/common导入LocationStrategy时,会显示此信息 这是我的spec.ts文件: student.component.spec.ts import {TestBed, ComponentFixture, async} from '@angular/core/
Angular 4/5
组件创建单元测试,其中我有很多导入。但在进口定位策略之后,我得到了:
TypeError:此.\u platformStrategy.getBaseHref不是函数
当我从@angular/common
导入LocationStrategy
时,会显示此信息
这是我的spec.ts
文件:
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);
});
});
从'@angular/core/testing'导入{TestBed,ComponentFixture,async};
从“rxjs/Observable”导入{Observable};
导入“rxjs/add/observable/of”;
从“/student.component”导入{StudentComponent};
从“/student.service”导入{StudentService};
从“./Student”导入{Student};
从“../shared/entities/RecordScont”导入{RecordScont};
从“../groups/Group”导入{Group};
从“../groups/groups.service”导入{GroupsService};
从'@angular/core'导入{NO_ERRORS_SCHEMA};
从'@angular/common/http'导入{HttpClient};
从'@angular/common/http'导入{HttpHandler};
从'@angular/cdk/overlay'导入{OverlayModule};
从“../main material.module”导入{main materialmodule};
从'@angular/common/http/testing'导入{HttpClientTestingModule};
从“../entity table/update delete entity.service”导入{UpdateDeleteEntityService};
从“../info modal/info modal.service”导入{InfoModalService};
从'@angular/router'导入{ActivatedRoute};
从“@angular/common”导入{Location,LocationStrategy};
描述('StudentComponent',()=>{
let夹具:组件夹具;
let成分:学生成分;
让模拟路由器={
导航:jasmine.createSpy('navigate')
};
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[StudentComponent],
模式:[无错误模式],
导入:[MainMaterialModule,HttpClientTestingModule],
供应商:[
学生服务,
组服务,
InfoModalService,
UpdateDeleteEntityService,
位置,
区位战略,
{提供:ActivatedRoute,useValue:mockRouter}]
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(StudentComponent);
组件=fixture.debugElement.componentInstance;
fixture.detectChanges();
});
它('应该返回学生数组',()=>{
const studentService=fixture.debugElement.injector.get(studentService);
fixture.detectChanges();
康斯特学生:学生[]=[
{
用户ID:20,
成绩册ID:'UX-3311221',
学生姓氏:“Лаааа”,
学生姓名:“Ааіа”,
学生名称:“Iаачч”,
groupId:'2',
密码:“”,
照片:“”
}, {
用户ID:13,
成绩册编号:“UY-3019273”,
学生姓氏:“аааааа”,
学生姓名:“Iааааа”,
学生姓名:“Гчччч”,
groupId:'5',
密码:“”,
照片:“”
}, {
用户ID:155,
成绩册编号:“UT-1029384”,
学生姓氏:“Ліаа”,
学生姓名:“АббббΓіМ”,
学生姓名:',
groupId:'1',
密码:“”,
照片:“”
}];
常数RecordScont:RecordScont={
numberOfRecords:'3'
};
const spy=spyOn(studentService,'getStudentsRange')。和.returnValue(可观察的([students,recordscont]);
组件。getStudents();
期望(学生),toEqual(学生);
});
});
您可能已经找到了解决此问题的方法,但这只是为了以防万一,也为了其他有同样问题的人的利益。我遇到了这个问题,发现以下解决方案有效:
我认为问题在于LocationStrategy是一个抽象类,您需要提供一个扩展LocationStrategy的具体类,比如PathLocationStrategy。我将以下内容添加到我的提供者列表中
{provide:LocationStrategy,useClass:PathLocationStrategy},
然后,因为PathLocationStrategy依赖于APP_BASE_REF,我还为此添加了一个提供程序
{提供:APP_BASE_HREF,useValue:'/my/APP'}
APP_BASE_HREF和PathLocationStrategy从@angular/common导入,与Location和LocationStrategy导入相同Lifesaver。谢谢