【问题标题】:Test Angular. TypeError: this._platformStrategy.getBaseHref is not a function测试角。 TypeError:this._platformStrategy.getBaseHref 不是函数
【发布时间】:2018-05-30 07:40:27
【问题描述】:

我想测试路由。我试过这个脚本:

describe('Router: App', () => {
    let location: Location;
    let router: Router;
    let fixture;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                AlarmsTableComponent,
                AddAlarmsFormComponent,
                EditAlarmsFormComponent,
                LoginComponent,
                DataFilterPipe
            ],
            imports: [RouterTestingModule, DataTableModule, ReactiveFormsModule, FormsModule, HttpClientTestingModule, HttpModule],
            providers: [Location, LocationStrategy, AlarmsService, DataFilterPipe, AuthService, ConnectionBackend, HttpModule,
                {
                    provide: APP_BASE_HREF, useValue: '/page' }
                ]
        });

        router = TestBed.get(Router);
        location = TestBed.get(Location);

        fixture = TestBed.createComponent(MainComponent);
        router.initialNavigation();
    });
    it('navigate to "" redirects you to /page', fakeAsync(() => {
        router.navigate(['page']);
        tick();
        expect(location.path()).toBe('/page');
    }));
});

当我ng test时结果是这个错误:

TypeError: this._platformStrategy.getBaseHref 不是函数

编辑:

describe('Router: App', () => {
    let location: Location;
    let router: Router;
    let fixture;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                AlarmsTableComponent,
                AddAlarmsFormComponent,
                EditAlarmsFormComponent,
                LoginComponent,
                DataFilterPipe
            ],
            imports: [RouterTestingModule, DataTableModule, ReactiveFormsModule, FormsModule, HttpClientTestingModule, HttpModule],
 providers: [Location, AuthService, AlarmsService, DataFilterPipe,
            {provide: LocationStrategy, useClass: PathLocationStrategy }],
        });

        router = TestBed.get(Router);
        location = TestBed.get(Location);

        fixture = TestBed.createComponent(MainComponent);
        router.initialNavigation();
    });
    it('navigate to "" redirects you to /page', fakeAsync(() => {
        router.navigate(['page']);
        tick();
        expect(location.path()).toBe('/page');
    }));
});

现在显示这个:

错误:未设置基本 href。请为 APP_BASE_HREF 提供一个值 标记或向文档添加基本元素。

【问题讨论】:

  • 你能告诉我们你在哪里初始化_platformStrategy吗?
  • 我还没有初始化

标签: angular unit-testing routing jasmine karma-jasmine


【解决方案1】:

首先,删除providers 中的HttpModule。他一定不在,就在imports

修改你提供LocationStrategy的方式,像这样:

{ provide: LocationStrategy, useClass: PathLocationStrategy },
{ provide: APP_BASE_HREF, useValue: '/page' }

PathLocationStrategy official doc上查看更多信息。

【讨论】:

  • 我改了,现在显示这个错误:Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.
  • 不要删除{ provide: APP_BASE_HREF, useValue: '/page' }。来自官方文档:“如果您使用 PathLocationStrategy,则必须提供 APP_BASE_HREF”
  • 在 routing.ts 我有这个 { path: '**', redirectTo: 'page' } 当我点击测试时,显示 NOT FOUND。
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2021-11-21
  • 2019-02-19
  • 2018-03-07
  • 1970-01-01
  • 2023-04-07
  • 2021-11-17
  • 2019-11-20
相关资源
最近更新 更多