如何用茉莉花对 OidcSecurityService.Auth() 进行单元测试?
How to unit test OidcSecurityService.Auth() with jasmine?
我正在使用 Jasmine 进行单元测试。我正在使用 OidcSecurityService。所以我为它做了一个模拟,像这样:
export class OidcSecurityServiceStub{
getToken(){
return 'some_token_eVbnasdQ324';
}
}
我有这个组件:
export class AutoLoginComponent implements OnInit {
lang: any;
constructor(public oidcSecurityService: OidcSecurityService) {}
/**
* responsible that user will be redirected to login screen.
*/
ngOnInit() {
this.oidcSecurityService.checkAuth().subscribe(() => this.oidcSecurityService.authorize());
}
}
这是规范现在的样子:
describe('AutoLoginComponent', () => {
let component: AutoLoginComponent;
let fixture: ComponentFixture<AutoLoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AutoLoginComponent ],
providers: [ {provide: OidcSecurityService, useClass: OidcSecurityServiceStub} ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AutoLoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create autologin component ', () => {
const fixture = TestBed.createComponent(AutoLoginComponent);
expect(component).toBeTruthy();
});
});
但是我得到这个错误:
NullInjectorError: R3InjectorError(DynamicTestModule)[OidcSecurityService -> OidcSecurityService]:
NullInjectorError: No provider for OidcSecurityService!
那么我必须改变什么?
谢谢
所以我做了这个改变:
import { AuthOptions } from 'angular-auth-oidc-client/lib/login/auth-options';
import { of } from 'rxjs';
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth(url: string) {
return of(url);
}
authorize(authOptions?: AuthOptions){
return authOptions.urlHandler('http://localhost');
}
}
但是我得到这个错误:
AfterAll TypeError: Cannot read property 'urlHandler' of undefined
在你的 ngOnInit
中,你调用 authorize
时没有任何参数,当它调用你存根的 authorize
时,它是未定义的。
您可以尝试将模拟更改为如下内容:
import { AuthOptions } from 'angular-auth-oidc-client/lib/login/auth-options';
import { of } from 'rxjs';
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;
}
}
}
我正在使用 Jasmine 进行单元测试。我正在使用 OidcSecurityService。所以我为它做了一个模拟,像这样:
export class OidcSecurityServiceStub{
getToken(){
return 'some_token_eVbnasdQ324';
}
}
我有这个组件:
export class AutoLoginComponent implements OnInit {
lang: any;
constructor(public oidcSecurityService: OidcSecurityService) {}
/**
* responsible that user will be redirected to login screen.
*/
ngOnInit() {
this.oidcSecurityService.checkAuth().subscribe(() => this.oidcSecurityService.authorize());
}
}
这是规范现在的样子:
describe('AutoLoginComponent', () => {
let component: AutoLoginComponent;
let fixture: ComponentFixture<AutoLoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AutoLoginComponent ],
providers: [ {provide: OidcSecurityService, useClass: OidcSecurityServiceStub} ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AutoLoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create autologin component ', () => {
const fixture = TestBed.createComponent(AutoLoginComponent);
expect(component).toBeTruthy();
});
});
但是我得到这个错误:
NullInjectorError: R3InjectorError(DynamicTestModule)[OidcSecurityService -> OidcSecurityService]:
NullInjectorError: No provider for OidcSecurityService!
那么我必须改变什么?
谢谢
所以我做了这个改变:
import { AuthOptions } from 'angular-auth-oidc-client/lib/login/auth-options';
import { of } from 'rxjs';
export class OidcSecurityServiceStub {
getToken() {
return 'some_token_eVbnasdQ324';
}
checkAuth(url: string) {
return of(url);
}
authorize(authOptions?: AuthOptions){
return authOptions.urlHandler('http://localhost');
}
}
但是我得到这个错误:
AfterAll TypeError: Cannot read property 'urlHandler' of undefined
在你的 ngOnInit
中,你调用 authorize
时没有任何参数,当它调用你存根的 authorize
时,它是未定义的。
您可以尝试将模拟更改为如下内容:
import { AuthOptions } from 'angular-auth-oidc-client/lib/login/auth-options';
import { of } from 'rxjs';
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;
}
}
}