我是单元测试的新手,并且有一些基础知识。但是,我正在尝试测试一种方法。此方法调用一个函数,该函数是 oidc-client.js 的一部分。它基本上让用户登录。
规格文件
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AuthCallbackComponent } from './auth-callback.component';
import { RouterTestingModule } from '@angular/router/testing';
fdescribe('AuthCallbackComponent', () => {
let component: AuthCallbackComponent;
let fixture: ComponentFixture<AuthCallbackComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [ AuthCallbackComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AuthCallbackComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
组件.ts
import { UserManager,User,UserManagerSettings,WebStorageStateStore} from 'oidc-client';
manager = new UserManager(this.getClientSettings());
ngOnInit() {
this.authService.completeAuthentication();
}
completeAuthentication(): Promise<void> {
return this.manager.signinRedirectCallback().then(user => {
this.user = user;
this.router.navigate(['/']);
this.setUser.next(this.user.profile);
this.onLogin.next(true)
});
}
getClientSettings() {
return {
authority: environment.authority,
client_id: environment.client_id,
redirect_uri: environment.login,
post_logout_redirect_uri: environment.logout,
response_type: 'code',
scope: 'openid profile email phone address',
filterProtocolClaims: true,
loadUserInfo: false,
accessTokenExpiringNotificationTime: 60,
silentRequestTimeout: 10000,
includeIdTokenInSilentRenew: true,
automaticSilentRenew: true,
silent_redirect_uri: environment.silent_redirect
};
}
}
我不确定如何测试这个。当我运行测试时,我只是得到'没有响应状态'。我希望测试通过,也许还有一些关于如何测试 completeAuthentication() 的想法