我正在尝试测试路由。 我有一个函数来检查我是否已登录,这是在Login / Logout按钮隐藏属性中作为语句给出的。 HTML: 登录
<a [hidden]="setLoggedOut()" routerLink="/login"
(click)="clearSessionStorage()">Logout</a>
登录/ LogoutChecker
setLoggedOut = () => {
let loggedOut = true;
const currentUser = sessionStorage.Status;
if (currentUser === 'ok') {
loggedOut = false;
}
return loggedOut;
}
在测试期间,我收到以下错误消息: TypeError:compo.setLOggedOut不是函数
这是测试组件:
import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { CanActivate, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { async, inject, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { Http, ConnectionBackend, RequestOptions } from '@angular/http';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { RoutingService } from './routing.service';
import { AppComponent } from './app.component';
class requestoptions {
public requestoption: RequestOptions
}
@Component ({
templateUrl: './app.component.html'
})
class RoutingComponent { }
describe('component: RoutingComponent', () => {
let location, routing, compo;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
RouterTestingModule.withRoutes([
{ path: 'login', component: LoginComponent },
{ path: '', component: HomeComponent, canActivate:
[RoutingService] }
])
],
declarations: [
AppComponent,
RoutingComponent,
HomeComponent,
LoginComponent
],
providers: [
RoutingService,
{provide: Http, useClass: requestoptions},
ConnectionBackend
]
});
});
beforeEach(inject([Router, Location], (_router: Router, _location:
Location) => {
location = _location;
routing = _router;
}));
it('should go home', async(() => {
const fixture = TestBed.createComponent(RoutingComponent);
fixture.detectChanges();
routing.navigate(['']).then(() => {
expect(location.path()).toBe('/');
console.log('after expect');
});
}));
it('should go to the login page', async(() => {
const fixture = TestBed.createComponent(RoutingComponent);
fixture.detectChanges();
routing.navigate(['login']).then(() => {
expect(location.path()).toBe('/login');
console.log('after expect');
});
}));
});