如何使用 then 块对返回 Promise 的函数进行单元测试
我有这段代码来使用 OAuth2 初始化身份验证,并在访问我的应用程序之前将用户重定向到身份验证服务器
import {Component} from '@angular/core';
import {JwksValidationHandler, OAuthService} from 'angular-oauth2-oidc';
import {authConfig} from './sso.config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor(private oauthService: OAuthService) {}
async initAuth(): Promise<any> {
return new Promise<void>((resolveFn, rejectFn) => {
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
if (this.oauthService.hasValidAccessToken()) { // ----------------> My test can't get in this part
this.oauthService.setupAutomaticSilentRefresh();
resolveFn();
}else {
this.oauthService.initCodeFlow();
rejectFn();
}
});// --------------------------------> It goes directly here and gives me: SPEC HAS NO EXPECTATIONS
});
}
}
我想使用 Jasmine 和 Karma 对这部分进行单元测试,我的测试如下所示:
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
const oauthServiceMock = {
oauthService: {
loadDiscoveryDocumentAndTryLogin(): Promise<true> {
return new Promise<true>(resolve => resolve());
}
}
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [OAuthModule.forRoot(), HttpClientTestingModule],
providers: [
{provide: AppComponent, useValue: oauthServiceMock}
],
declarations: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('Test for initAuth', fakeAsync(() => {
const spy = spyOn(oauthServiceMock.oauthService, 'loadDiscoveryDocumentAndTryLogin').and.returnValue(Promise.resolve(true));
component.initAuth().then(() => {
tick(5000);
expect(spy).toHaveBeenCalled();
});
}));
});
我的测试无法进入块然后我不知道我错过了什么。
I have this code to initialize an authentication using OAuth2 and redirect the user to an authentication server before getting to my application
import {Component} from '@angular/core';
import {JwksValidationHandler, OAuthService} from 'angular-oauth2-oidc';
import {authConfig} from './sso.config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor(private oauthService: OAuthService) {}
async initAuth(): Promise<any> {
return new Promise<void>((resolveFn, rejectFn) => {
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
if (this.oauthService.hasValidAccessToken()) { // ----------------> My test can't get in this part
this.oauthService.setupAutomaticSilentRefresh();
resolveFn();
}else {
this.oauthService.initCodeFlow();
rejectFn();
}
});// --------------------------------> It goes directly here and gives me: SPEC HAS NO EXPECTATIONS
});
}
}
I would like to unit test this part using Jasmine and Karma, my test looks like:
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
const oauthServiceMock = {
oauthService: {
loadDiscoveryDocumentAndTryLogin(): Promise<true> {
return new Promise<true>(resolve => resolve());
}
}
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [OAuthModule.forRoot(), HttpClientTestingModule],
providers: [
{provide: AppComponent, useValue: oauthServiceMock}
],
declarations: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('Test for initAuth', fakeAsync(() => {
const spy = spyOn(oauthServiceMock.oauthService, 'loadDiscoveryDocumentAndTryLogin').and.returnValue(Promise.resolve(true));
component.initAuth().then(() => {
tick(5000);
expect(spy).toHaveBeenCalled();
});
}));
});
My test can't get into the block then I don't know what am I missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您错误地提供了模拟函数。
更改为:
更改
为: 更改
为:
我们需要为
OAuthService
而不是AppComponent
提供模拟。这应该有望解决您的问题。I think you're providing the mock function incorrectly.
Change this:
To this:
And change this:
To this:
We need to provide a mock to
OAuthService
and notAppComponent
. That should hopefully resolve your issue.