我有一个 Angular 8 应用程序,我用 jasmine karma 做了一些单元测试。这是 component.ts:
export class DossierPersonalDataComponent implements OnInit {
dossier: DossierDto;
editDossierForm: FormGroup;
formBuilder = new FormBuilder();
globalEditDossierErrors: ValidationErrors;
dossierItems: DossierItemDto[] = [];
profileImagefile: File;
profileImageNeedsUpload = false;
constructor(
private dossierService: DossierService,
private route: ActivatedRoute,
private sanitizer: DomSanitizer,
private dossierFileService: DossierFileService,
private errorProcessor: ErrorProcessor,
private dialog: MatDialog
) {
this.dossierItems = this.route.snapshot.data.dossierItems;
this.editDossierForm = this.formBuilder.group({});
this.editDossierForm.disable();
this.dossier = this.route.snapshot.data.dossier;
this.dossierItems = route.snapshot.data.dossierItems;
this.profileImagefile = this.route.snapshot.data.profileImage;
this.editDossierForm = this.formBuilder.group({
firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
});
}
ngOnInit(): void {
this.editDossierForm.disable();
}
editName() {
this.editDossierForm.enable();
}
get profileImageUrl() {
return this.profileImagefile === null
? '/assets/placeholder.jpg'
: this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
}
}
这是规范文件:
describe('DossierPersonalDataComponent', () => {
let component: DossierPersonalDataComponent;
let fixture: ComponentFixture<DossierPersonalDataComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, DossierModule, BrowserModule],
declarations: [DossierPersonalDataComponent],
providers: [
DossierFileService,
ErrorProcessor,
{
provide: ActivatedRoute,
useValue: {
snapshot: {
data: {
dossier: {
firstName: 'hello',
lastName: 'world',
mobile: '111-111-1111',
company: 'carapax',
buddy: 'bud',
supervisor: 'super',
dateOfBirth: '1900-01-01',
},
dossierItems: [], // mock
profileImage: '',
}
}
}
},
{
provide: DomSanitizer,
useValue: {
sanitize: () => 'safeString',
bypassSecurityTrustHtml: () => 'safeString'
}
}
]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(DossierPersonalDataComponent);
component = fixture.componentInstance;
});
}));
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
});
但是当我运行测试时,我收到此错误:
DossierPersonalDataComponent > should create
TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
在覆盖文件中,我将其视为黄色:
get profileImageUrl() {
return this.profileImagefile === null
? '/assets/placeholder.jpg'
: this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
}
所以这是黄色部分
? '/assets/placeholder.jpg'
分支未覆盖。
那么我必须改变什么?
谢谢
我正在使用谷歌浏览器
我改成这样:
provide: ActivatedRoute,
useValue: {
snapshot: {
data: {
dossier: {
firstName: 'hello',
lastName: 'world',
mobile: '111-111-1111',
company: 'carapax',
buddy: 'bud',
supervisor: 'super',
dateOfBirth: '1900-01-01',
},
dossierItems: [], // mock
profileImagefile: null,
}
}
}
但没有变化