我应该如何为这些查询参数的if语句写出单元测试if语句



.ts文件:

ngOnInit(): void {
if (
this.activatedRoute.snapshot.queryParamMap.has('esign') && 
this.activatedRoute.snapshot.queryParamMap.has('registration')
) {
this.eSignAndRegistration = true;
}

.spec.ts文件:

let esign_registration = false;
const mockActivatedRoute = {
snapshot: {
queryParamMap: {
has: (response) => {
if (response == 'esign' && response == 'registration') {
return esign_registration;
}

测试:

fit("should show the 1st mat-card if the param is 'esign&registration'", () => {
esign_registration = true;
fixture.detectChanges();
component.ngOnInit();
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(component.eSignAndRegistration).toBeTruthy();
});

错误不断出现;期望未定义是真实的"如何编写"has"if语句才能使单元测试正常工作?我试过

if (response == 'esign' && 'registration')

if (response == 'esign&registration')

以及各种各样的其他方法,但它们都不断给出相同的错误。

response == 'esign' && response == 'registration'

尝试将&&更改为||。如果响应是一个字符串,它只能是其中之一。

最新更新