如何使用 Jasmine 对 OidcSecurityService.checkAuth 函数进行单元测试?
How to unit testing OidcSecurityService.checkAuth function with Jasmine?
我在 Angular 中使用 Jasmine 进行单元测试。为了安全起见,我正在使用 OidcSecurityService 服务。现在我想对自动登录这块进行单元测试:
export class AppComponent implements OnInit {
title = 'cityflows-client';
constructor(public oidcSecurityService: OidcSecurityService, private router: Router, public platform: Platform) {}
ngOnInit() {
this.oidcSecurityService
.checkAuth()
.subscribe(isAuthenticated => {
if (!isAuthenticated) {
if ('/autologin' !== window.location.pathname) {
this.write('redirect', window.location.pathname);
this.router.navigate(['/autologin']);
}
}
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
});
}
因此单元测试如下所示:
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
providers: [ {provide: OidcSecurityService, useClass: OidcSecurityServiceStub} ],
declarations: [
AppComponent
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the app', () => {
expect(component).toBeTruthy();
});
});
存根看起来像这样:
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth(url: string) {
return of(url);
}
authorize(authOptions?: AuthOptions) {
if (authOptions) {
return authOptions.urlHandler('http://localhost');
} else {
return null;
}
}
}
但我在报道中看到:
通过 if 表示 else 部分没有被占用。
那么我必须改变什么?谢谢
it('should redirect user to login page if user is not logged in', () => {
expect(component).toBeTruthy();
});
- 您需要更正模拟文件
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth() {
return of(true); // <-------- make it return true
}
authorize(authOptions?: AuthOptions) {
if (authOptions) {
return authOptions.urlHandler('http://localhost');
} else {
return null;
}
}
}
这将涵盖以下代码:
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
现在 false
,使用 jasmine
it('Navigate if not authenticated', () => {
spyOn(component.oidcSecurityService,'checkAuth').and.returnValue(of(false));
// also spy on write and router.navigate
component.ngOnInit()
expect(component.router.navigate).not.toHaveBeenCalledWith(['/autologin']); // because the url will not be /authLogin (I am assuming, please correct accordingly)
// similarly check for component.write()
});
- 要检查
if ('/autologin' !== window.location.pathname)
,您需要覆盖 window
对象以在 pathName
中相应地设置值
您可以参考 我在其中模拟了 window
方法之一。在你的情况下你需要设置 pathName
并测试它
我在 Angular 中使用 Jasmine 进行单元测试。为了安全起见,我正在使用 OidcSecurityService 服务。现在我想对自动登录这块进行单元测试:
export class AppComponent implements OnInit {
title = 'cityflows-client';
constructor(public oidcSecurityService: OidcSecurityService, private router: Router, public platform: Platform) {}
ngOnInit() {
this.oidcSecurityService
.checkAuth()
.subscribe(isAuthenticated => {
if (!isAuthenticated) {
if ('/autologin' !== window.location.pathname) {
this.write('redirect', window.location.pathname);
this.router.navigate(['/autologin']);
}
}
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
});
}
因此单元测试如下所示:
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
providers: [ {provide: OidcSecurityService, useClass: OidcSecurityServiceStub} ],
declarations: [
AppComponent
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the app', () => {
expect(component).toBeTruthy();
});
});
存根看起来像这样:
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth(url: string) {
return of(url);
}
authorize(authOptions?: AuthOptions) {
if (authOptions) {
return authOptions.urlHandler('http://localhost');
} else {
return null;
}
}
}
但我在报道中看到:
通过 if 表示 else 部分没有被占用。
那么我必须改变什么?谢谢
it('should redirect user to login page if user is not logged in', () => {
expect(component).toBeTruthy();
});
- 您需要更正模拟文件
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth() {
return of(true); // <-------- make it return true
}
authorize(authOptions?: AuthOptions) {
if (authOptions) {
return authOptions.urlHandler('http://localhost');
} else {
return null;
}
}
}
这将涵盖以下代码:
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
现在 false
,使用 jasmine
it('Navigate if not authenticated', () => {
spyOn(component.oidcSecurityService,'checkAuth').and.returnValue(of(false));
// also spy on write and router.navigate
component.ngOnInit()
expect(component.router.navigate).not.toHaveBeenCalledWith(['/autologin']); // because the url will not be /authLogin (I am assuming, please correct accordingly)
// similarly check for component.write()
});
- 要检查
if ('/autologin' !== window.location.pathname)
,您需要覆盖window
对象以在pathName
中相应地设置值
您可以参考 window
方法之一。在你的情况下你需要设置 pathName
并测试它