Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 5-当尝试导航到特定路线时,Jasmine的路线测试失败_Angular_Unit Testing_Routing_Jasmine_Angular5 - Fatal编程技术网

Angular 5-当尝试导航到特定路线时,Jasmine的路线测试失败

Angular 5-当尝试导航到特定路线时,Jasmine的路线测试失败,angular,unit-testing,routing,jasmine,angular5,Angular,Unit Testing,Routing,Jasmine,Angular5,我使用Angular 5,我正在用Jasmine&Karma测试Angular服务。 我有一个身份验证服务,其功能包括一个可以重定向到登录页面的功能。如果用户没有经过身份验证,我就调用它 我尝试了这个函数,它执行了一个简单的“router.navigate['login']”操作,但出现了以下错误: Chrome 62.0.3202 (Linux 0.0.0) AuthenticationService should redirect to login page FAILED E

我使用Angular 5,我正在用Jasmine&Karma测试Angular服务。 我有一个身份验证服务,其功能包括一个可以重定向到登录页面的功能。如果用户没有经过身份验证,我就调用它

我尝试了这个函数,它执行了一个简单的“router.navigate['login']”操作,但出现了以下错误:

Chrome 62.0.3202 (Linux 0.0.0) AuthenticationService should redirect to login page FAILED
        Expected '' to be '/login'.
            at Object.<anonymous> home/user/Git/astre-fo/src/app/services/authentification.service.spec.ts:171:29)
            at Object.<anonymous> home/user/Git/astre-fo/node_modules/@angular/core/esm5/testing.js:411:1)
            at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/user/Git/astre-fo/node_modules/zone.js/dist/zone.js:388:1)
            at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke home/user/Git/astre-fo/node_modules/zone.js/dist/proxy.js:79:1)
Chrome 62.0.3202 (Linux 0.0.0): Executed 17 of 17 (1 FAILED) (0 secs / 1.922 secs)
Chrome 62.0.3202 (Linux 0.0.0) AuthenticationService should redirect to login page FAILED
        Expected '' to be '/login'.
            at Object.<anonymous> home/user/Git/astre-fo/src/app/services/authentification.service.spec.ts:171:29)
            at Object.<anonymous> home/user/Git/astre-fo/node_modules/@angular/core/esm5/testing.js:411:1)
            at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/user/Git/astre-fo/node_modules/zone.js/dist/zone.js:388:1)
Chrome 62.0.3202 (Linux 0.0.0): Executed 17 of 17 (1 FAILED) (2.031 secs / 1.922 secs)
authentication.service.ts:

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {UserService} from './user.service';
import {Router} from '@angular/router';
import {Location} from '@angular/common';

@Injectable()
export class AuthenticationService {

  constructor(
    private httpClient: HttpClient,
    public userService: UserService,
    private router: Router,
    private location: Location
  ) { }

  public redirectToLogin() {
    this.router.navigate(['login']);
  }
}
app.routes.ts:

import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { AuthGuardService } from './services/auth-guard.service';
import { SearchComponent } from './search/search.component';

export const routes: Routes = [
    { path: '', redirectTo: 'search', pathMatch: 'full'},
    { path: 'login', component: LoginComponent },
    { path: 'search', component: SearchComponent, canActivate: [AuthGuardService] }
];

export const routing = RouterModule.forRoot(routes);
有人有主意吗

this.router.navigate(['/login']);

通过/它会起作用

我找到了一个解决方案:使用承诺

  it('should redirect to login page', fakeAsync(() => {

    service.redirectToLogin().then(() => {
      expect(location.path()).toBe('/login');
    });
  }));
如果有人有更好的解决方案,或者如果我的解决方案不正确,请告诉我:)

  it('should redirect to login page', fakeAsync(() => {

    service.redirectToLogin().then(() => {
      expect(location.path()).toBe('/login');
    });
  }));