【发布时间】:2021-09-14 06:43:46
【问题描述】:
我目前正在对我的应用程序进行 Angular 单元测试,但遇到了需要对本地文件使用 http 调用的问题。因此,测试的期望在 http 调用之前和之后被调用,所以它崩溃了。我该如何解决这个问题?
我目前正在尝试在 http 调用的 .subscribe 中调用期望:
fit('should get local file and work with it', (done: DoneFn) => {
let file: File;
var blob: Blob;
httpMockService.get('/assets/testFiles/testImage.png', {
responseType: 'blob' }).subscribe((resp: any) => {
blob = resp
file = new File([blob], 'logo.png',
{ type: 'image/png', lastModified: Date.now() });
component.submitFile(file); <- get called seecond
expect(component.canBeProgressed).toEqual(true); <- get called first
expect(component.submited).toEqual(false);
done();
});
问题是期望值是错误的,因为 component.submitFile 在期望之后被调用。我用间谍尝试过,但这不起作用。谁能帮帮我?
编辑:在 submitFile() 方法中包含一个 FileReader,它加载图像并使用其像素。我认为这会导致问题
【问题讨论】:
-
这能回答你的问题吗:stackoverflow.com/a/60438097/2358409
标签: angular unit-testing http karma-jasmine