Angular 12单元测试:无法执行'附录' on formdata;:x27;:参数2不是类型' blob'
我正在尝试为组件方法编写一个单元测试,但是我会遇到此错误:
typeError:无法在'formData'上执行'附录':参数2不是类型'blob'。
我找不到对我有用的答案。
方法:
public changeProfilePicture(image: File) {
let formData: FormData = new FormData();
formData.append('file', image, image.name);
this.store.dispatch(ProfilePictureActions.update({ formData, userId: this.userId }));
}
单位测试:
it('changed profile picture', () => {
const mockStoreSpy = spyOn(mockStore, 'dispatch');
const blobImage: Blob = new Blob(['image'] , { type: 'image/jpg' });
const image: File = { ...blobImage, name: 'pictureName.jpg', lastModified: 1 };
let formData: FormData = new FormData();
formData.append('file', <Blob>image, image.name);
component.changeProfilePicture(image);
expect(mockStoreSpy).toHaveBeenCalledWith(ProfilePictureActions.update({ formData, userId }));
});
I'm trying to write a unit test for the component method, but I'm getting this error:
TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.
I conldn't find an answer that worked for me.
Method:
public changeProfilePicture(image: File) {
let formData: FormData = new FormData();
formData.append('file', image, image.name);
this.store.dispatch(ProfilePictureActions.update({ formData, userId: this.userId }));
}
Unit test:
it('changed profile picture', () => {
const mockStoreSpy = spyOn(mockStore, 'dispatch');
const blobImage: Blob = new Blob(['image'] , { type: 'image/jpg' });
const image: File = { ...blobImage, name: 'pictureName.jpg', lastModified: 1 };
let formData: FormData = new FormData();
formData.append('file', <Blob>image, image.name);
component.changeProfilePicture(image);
expect(mockStoreSpy).toHaveBeenCalledWith(ProfilePictureActions.update({ formData, userId }));
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这解决了我的问题。
const Image:file = new File([BlobImage],'Picturename.jpg',{type:'Image/jpg'});
This solved my problem.
const image: File = new File([blobImage], 'pictureName.jpg', { type: 'image/jpg' });