使用 Jest 获取错误测试读取流响应
Getting error testing readstream response with Jest
我在测试 returns 从 google 云存储中读取流的函数时出现超时错误。
// Test case
import { createResponse } from 'node-mocks-http';
it('getImage from google cloud storage', async done => {
let res: Response = createResponse();
let data = await controller.getImage({ image: "text data"},object, response);
});
// Function that calls google gloud storage and return object.
public async getImage(imageReqDto, baseDto, response){
let bucket = this.storage.bucket(process.env.IMAGE_BUCKET);
let file = bucket.file(image);
return file.createReadStream().pipe(response);;
}
请问如何在收到buffer数据后成功通过测试用例
这取决于您要测试的内容。如果您只是想确保返回 ReadStream,请尝试使用 .toBeInstanceOf()
:
it('readstream is returned, when .getImage() is invoked with image data', async () => {
const response: Response = createResponse();
const stream = await controller.getImage({image: "text data"}, object, response);
expect(stream).toBeInstanceOf(ReadStream)
});
然而,更好的测试是了解预期响应并断言 ReadStream 与预期值匹配。如果它是一个小图像,您可以将其加载到缓冲区中并进行比较。如果图片比较大,可以考虑转成base64格式,对比实际和预期。
我在测试 returns 从 google 云存储中读取流的函数时出现超时错误。
// Test case
import { createResponse } from 'node-mocks-http';
it('getImage from google cloud storage', async done => {
let res: Response = createResponse();
let data = await controller.getImage({ image: "text data"},object, response);
});
// Function that calls google gloud storage and return object.
public async getImage(imageReqDto, baseDto, response){
let bucket = this.storage.bucket(process.env.IMAGE_BUCKET);
let file = bucket.file(image);
return file.createReadStream().pipe(response);;
}
请问如何在收到buffer数据后成功通过测试用例
这取决于您要测试的内容。如果您只是想确保返回 ReadStream,请尝试使用 .toBeInstanceOf()
:
it('readstream is returned, when .getImage() is invoked with image data', async () => {
const response: Response = createResponse();
const stream = await controller.getImage({image: "text data"}, object, response);
expect(stream).toBeInstanceOf(ReadStream)
});
然而,更好的测试是了解预期响应并断言 ReadStream 与预期值匹配。如果它是一个小图像,您可以将其加载到缓冲区中并进行比较。如果图片比较大,可以考虑转成base64格式,对比实际和预期。