Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2测试路由器模块_Angular_Angular Routing_Angular2 Testing - Fatal编程技术网

Angular 角度2测试路由器模块

Angular 角度2测试路由器模块,angular,angular-routing,angular2-testing,Angular,Angular Routing,Angular2 Testing,我正在尝试在Angular 2.2.1中测试我的路由器,以确保设置了正确的路由并到达预期位置。我已经做过谷歌研究,并且已经阅读并重新阅读了来自于的示例中的app.component.router.spec.ts文件,但仍然有不足之处 我在这里找到了一个非常好的答案,很容易理解,而且很明显: 因此,我用它为自己的路由器创建了一个测试: import { Component } from '@angular/core'; import { Location } from '@angular/com

我正在尝试在Angular 2.2.1中测试我的路由器,以确保设置了正确的路由并到达预期位置。我已经做过谷歌研究,并且已经阅读并重新阅读了来自于的示例中的app.component.router.spec.ts文件,但仍然有不足之处

我在这里找到了一个非常好的答案,很容易理解,而且很明显:

因此,我用它为自己的路由器创建了一个测试:

import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { fakeAsync, async, inject, TestBed, getTestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

@Component({
    template: '<router-outlet></router-outlet>'
})
class RoutingComponent { }

@Component({
    template: ''
})
class DummyComponent { }

describe('component: RoutingComponent', () => {
    let location, router;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [RouterTestingModule.withRoutes([
                { path: 'home', component: DummyComponent },
                { path: 'not-exists', component: DummyComponent }
            ])],
            declarations: [RoutingComponent, DummyComponent]
        });
    });

    beforeEach(inject([Router, Location], (_router: Router, _location: Location) => {
        location = _location;
        router = _router;
    }));

    it('should fail, but does not', async(() => {
        let fixture = TestBed.createComponent(RoutingComponent);
        fixture.detectChanges();
        router.navigate(['not-exists']).then(() => {
            expect(location.path()).toBe('/not-exists');
            console.log('after expect');
        });
    }));
});
从'@angular/core'导入{Component};
从“@angular/common”导入{Location};
从'@angular/Router'导入{Router};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/core/testing'导入{fakeAsync,async,inject,TestBed,getTestBed};
从“@angular/platform browser”导入{By}”;
@组成部分({
模板:“”
})
类路由组件{}
@组成部分({
模板:“”
})
类DummyComponent{}
描述('组件:路由组件',()=>{
让定位,路由器;
在每个之前(()=>{
TestBed.configureTestingModule({
导入:[RouterTestingModule.withRoutes([
{路径:'home',组件:DummyComponent},
{路径:“不存在”,组件:DummyComponent}
])],
声明:[路由组件,DummyComponent]
});
});
beforeach(注入([Router,Location],(\u Router:Router,\u Location:Location)=>{
位置=_位置;
路由器=_路由器;
}));
它('应该失败,但不会',异步(()=>{
让fixture=TestBed.createComponent(RoutingComponent);
fixture.detectChanges();
路由器。导航(['not-exists'])。然后(()=>{
expect(location.path()).toBe(“/不存在”);
log('after expect');
});
}));
});
我的问题是,我不明白这是如何测试我真正的路由器。正如您所见,我在这里创建了一个名为“notexists”的路径,该路径指向一个虚拟组件,正如名称所示,该路由在我的真实路由器中不存在。这让我相信,它真的没有测试任何东西,除了我放在这里的路径

因此,我的问题是:

  • 如何测试我的真实路由器,以便在路由发生变化时中断测试
  • 我如何测试我的路由器,它去一个路径,但重定向到另一个路径?(例如,我有一个重定向到“/home”的路径“/”)
  • 是否有可能测试我的真实路由器,其中特定路径指向特定组件

  • 谢谢

    我会端到端地测试路由,而不是作为单元测试;它需要多个组件协同工作。