gpt4 book ai didi

Angular :单元测试路由:预期 '' 为 '/route'

转载 作者:太空狗 更新时间:2023-10-29 17:45:18 25 4
gpt4 key购买 nike

我正在为我的 Angular 应用程序下的路由进行单元测试,

我的路线在 app.module.ts 下导入的特定模块中声明,

这是我的路由模块:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { LoginComponent } from './login/login.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { CustomersListComponent } from './customer/customers-list/customers-list.component';
import { CustomerDetailComponent } from './customer/customer-detail/customer-detail.component';
import { ApplicationParametersComponent } from './superAdministrator/application-parameters/application-parameters.component';
import { InscriptionComponent } from './inscription/inscription.component';

const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'login/:keyWording', component: LoginComponent },
{ path: 'welcome', component: WelcomeComponent },
{ path: 'customers-list', component: CustomersListComponent },
{ path: 'customer-create', component: CustomerDetailComponent },
{ path: 'customer-detail/:idCustomer', component: CustomerDetailComponent },
{ path: 'application-parameters', component: ApplicationParametersComponent },
{ path: 'inscription', component: InscriptionComponent }
];

@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}

这是我的 app.module.ts(我用来导入路由模块的地方:

import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { SharedModule } from './../shared/shared.module';
import { LoginComponent } from './login/login.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { AppComponent } from './app.component';
import { CustomerModule } from './customer/customer.module';
import { ApplicationParametersComponent } from './superAdministrator/application-parameters/application-parameters.component';
import { InscriptionComponent } from './inscription/inscription.component';

import { DxProgressBarModule } from 'devextreme-angular';

@NgModule({
declarations: [
AppComponent,
LoginComponent,
WelcomeComponent,
ApplicationParametersComponent,
InscriptionComponent
],
imports: [
AppRoutingModule, /* HERE IS THE ROUTING FILE */

SharedModule,
CustomerModule,
DxProgressBarModule/*,
BrowserAnimationsModule,
BrowserModule*/
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

在我的测试文件下,我遵循了这个博客中的教程: https://codecraft.tv/courses/angular/unit-testing/routing/

我的路由测试文件如下:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
// DevExtreme Module
import {DxProgressBarModule, DxTemplateModule} from 'devextreme-angular';
// Router Modules
import {RouterTestingModule} from '@angular/router/testing';
// Services and HTTP Module
import { SessionService } from './../shared/service';
import { HttpService } from './../shared/service';
import {HttpModule} from '@angular/http';
// Routs testing
import {Router, RouterModule} from '@angular/router';
import {fakeAsync, tick} from '@angular/core/testing';
import {Location} from '@angular/common';
import {LoginComponent} from './login/login.component';
import {WelcomeComponent} from './welcome/welcome.component';
import {ApplicationParametersComponent} from './superAdministrator/application-parameters/application-parameters.component';
import {InscriptionComponent} from './inscription/inscription.component';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {FormsModule} from '@angular/forms';


describe('Testing the application routes', () => {

let location: Location;
let router: Router;
let fixture;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, FormsModule , DxTemplateModule , HttpModule ],
providers: [SessionService , HttpService ],
declarations: [
AppComponent,
LoginComponent,
WelcomeComponent,
ApplicationParametersComponent,
InscriptionComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]

});

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

fixture = TestBed.createComponent(AppComponent);
router.initialNavigation();
});

it('navigate to "inscription" takes you to /inscription', fakeAsync(() => {
router.navigate(['inscription']);
tick();
expect(location.path()).toBe('/inscription');
}));
});

我的测试失败,表明:

Expected '' to be '/inscription'.
at Object.<anonymous> (webpack:///src/app/app-routing.spec.ts:52:28 <- src/test.ts:143891:33)
at Object.<anonymous> (webpack:///~/@angular/core/@angular/core/testing.es5.js:348:0 <- src/test.ts:34691:26)
at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:391:0 <- src/polyfills.ts:1546:26)
at ProxyZoneSpec.Array.concat.ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- src/test.ts:232357:39)

想法??

最佳答案

您忘记将路由导入 RouterTestingModule , 在你的测试文件中。

您必须添加 export您的 const routes 的关键字在你的AppRoutingModule文件,那么你可以import测试文件中的路由(并将它们添加到测试配置中)。

import {routes} from '...'; // I don't have the app-routing.module file path.
...
...
...
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes(routes), <-- I added the routes here.
FormsModule , DxTemplateModule , HttpModule
],
providers: [SessionService , HttpService ],
declarations: [
AppComponent,
LoginComponent,
WelcomeComponent,
ApplicationParametersComponent,
InscriptionComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]

});

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

fixture = TestBed.createComponent(AppComponent);
router.initialNavigation();
});

如果您不在路由器测试模块中加载路由,当您navigate 时它将无法知道去哪里。 , 因此它将返回到原始页面并在控制台中显示错误。

你遵循的教程有一个非常奇怪的方式来处理路由,因为 tick()用于fakeAsync测试,这是一个真实的 async一。所以你必须使用 Promise<boolean>router.navigate 返回:

it('navigate to "inscription" takes you to /inscription', () => {
router.navigate(['inscription']).then(() => {
expect(location.path()).toBe('/inscription');
});
});

如您所见,您还可以删除 fakeAsync因为这不是假的,所以它是 async打电话。

See it on plunkr

关于 Angular :单元测试路由:预期 '' 为 '/route',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45564557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com