如何使用 Jasmine 测试 Angular 中的路由是否存在?
How to test the existence of a route in Angular using Jasmine?
我正在尝试编写一个测试,使用 Jasmine 为 Angular 应用程序检查路由是否存在:
import { routes } from './app-routing.module';
import { UsersComponent } from './users/users.component';
describe('routes', () => {
it('should contain route for /users', () => {
const expectedRoute = { path: 'users', component: UsersComponent };
expect(routes).toContain(expectedRoute);
});
});
但测试失败(可能是因为 Javascript How object equality checks work in Javascript? 中的对象相等)
如何修改测试以使其工作?
我试过了,你的代码很好用。
方法toContain
.
中的相等没有问题
您是否已将 app-routing.module.ts
中的 const routes
更改为 export const routes
,并在测试中将 import { routes } from './app-routing.module'
更改为 import { routes } from './app-routing.module'
?
这是我为测试应用程序而必须更改的唯一内容。
我已经完成了以下步骤:
ng new test-routing
(对路由说是,因此创建了 app-routing.module.ts
。
- 使用以下代码修改
app.component.spec.ts
:
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { routes } from './app-routing.module';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should contain route for /principal', () => {
const expectedRoute = { path: 'principal', component: AppComponent };
expect(routes).toContain(expectedRoute);
});
});
- 修改了
app-routing.module.ts
以添加此路由:
export const routes: Routes = [
{ path: '', redirectTo: '/principal', pathMatch: 'full' },
{ path: 'principal', component: AppComponent }
];
- 运行
ng test
测试没问题
如果我将 'principal'
路径更改为其他路径,测试将失败。
如果我恢复到好的状态,测试就可以了。
我正在尝试编写一个测试,使用 Jasmine 为 Angular 应用程序检查路由是否存在:
import { routes } from './app-routing.module';
import { UsersComponent } from './users/users.component';
describe('routes', () => {
it('should contain route for /users', () => {
const expectedRoute = { path: 'users', component: UsersComponent };
expect(routes).toContain(expectedRoute);
});
});
但测试失败(可能是因为 Javascript How object equality checks work in Javascript? 中的对象相等)
如何修改测试以使其工作?
我试过了,你的代码很好用。
方法toContain
.
您是否已将 app-routing.module.ts
中的 const routes
更改为 export const routes
,并在测试中将 import { routes } from './app-routing.module'
更改为 import { routes } from './app-routing.module'
?
这是我为测试应用程序而必须更改的唯一内容。
我已经完成了以下步骤:
ng new test-routing
(对路由说是,因此创建了app-routing.module.ts
。- 使用以下代码修改
app.component.spec.ts
:
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { routes } from './app-routing.module';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should contain route for /principal', () => {
const expectedRoute = { path: 'principal', component: AppComponent };
expect(routes).toContain(expectedRoute);
});
});
- 修改了
app-routing.module.ts
以添加此路由:
export const routes: Routes = [
{ path: '', redirectTo: '/principal', pathMatch: 'full' },
{ path: 'principal', component: AppComponent }
];
- 运行
ng test
测试没问题
如果我将 'principal'
路径更改为其他路径,测试将失败。
如果我恢复到好的状态,测试就可以了。