我正在编写单元测试,该函数将迭代存储容器中的blob。我目前在使用jest和jest-mock-extended来模拟listBlobsByHierarchy方法时遇到问题。是否有任何有关如何执行此操作的代码示例?我的实现未完成,但是我想要的行为是模拟listBlobsByHierarchy的实现以返回预期的BlobItem。我正在尝试返回模拟的ContainerListBlobHierarchySegmentResponse,但是我不确定如何将期望的blobItem附加到该对象,也不确定如何正确模拟该响应。
这是我到目前为止所拥有的。
我正在测试的方法:
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import { ContainerListBlobsOptions } from "@azure/storage-blob";
import { getStorageContainerClient } from "../blobStorage/BlobStorage";
import { ImageInfo } from "../common/types";
import { handleError } from "../cosmos/CosmosContext";
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void>
{
try
{
const containerClient = await getStorageContainerClient();
if(req.method === "GET")
{
const imageinfo: ImageInfo[] = [];
const containerListBlobsOptions: ContainerListBlobsOptions =
{
includeDeleted: true,
includeMetadata: true
};
for await (const item of containerClient.listBlobsByHierarchy("/", containerListBlobsOptions))
{
if (item.kind === "prefix")
{
context.log("Prefix type:");
context.log(item.name);
}
else
{
context.log(item.name);
context.log(item.deleted);
context.log(item.properties);
}
}
// Get a block blob client
// const blockBlobClient = containerClient.getBlockBlobClient(tenantBlobName);
// const properties = await blockBlobClient.getProperties();
context.res = { status: 200 };
// context.res = { status: 200, body: JSON.stringify(properties)};
return;
}
}
catch(err)
{
handleError(context, err);
}
};
export default httpTrigger;
到目前为止我的测试:
jest.mock("../../blobStorage/BlobStorage");
import { HttpRequest } from "@azure/functions";
import * as imageinfoFunction from "../../imageinfo/index";
import { context } from "../data/setupData";
import { BlobItem, ContainerClient, ContainerListBlobHierarchySegmentResponse } from "@azure/storage-blob";
import * as BlobStorage from "../../blobStorage/BlobStorage";
import { mock } from "jest-mock-extended";
import { ImageInfo } from "../../common/types";
describe("Image info tests", () =>
{
const getReq: HttpRequest = {
method: "GET",
url: "localhost.com",
headers: {"a": "b"},
query: {},
params: {"a": "b"},
};
const mockStorageContainerClient = mock<ContainerClient>();
const getStorageContainerClientSpy = jest.spyOn(BlobStorage, "getStorageContainerClient");
getStorageContainerClientSpy.mockImplementation(async () => mockStorageContainerClient );
afterEach(() => jest.clearAllMocks());
test("Calling image info function should return an array of image infos", async () =>
{
const listBlobsByHierarchySpy = jest.spyOn(mockStorageContainerClient, "listBlobsByHierarchy");
const thumbnail: Buffer = Buffer.from("test");
const imageInfo: ImageInfo =
{
filename: "file",
mimeType: "image/jpeg",
thumbnail,
thumbnailUrl: "thumbnail.url",
url: "imageUrl",
user: "user",
size: thumbnail.length,
createdOn: new Date("09/09/2020"),
deleted: false
};
const listBlobsByHierarchyResponse: ContainerListBlobHierarchySegmentResponse = mock<ContainerListBlobHierarchySegmentResponse>();
const blobItem: BlobItem =
{
name: "file",
deleted: false,
snapshot: "snapshot",
metadata: { user: "user"},
properties:
{
lastModified: new Date("09/09/2020"),
etag: "etag",
createdOn: new Date("09/09/2020"),
contentLength: thumbnail.length,
contentType: "image/jpeg"
}
};
listBlobsByHierarchySpy.mockResolvedValue(() => listBlobsByHierarchyResponse);
const expectedResult: ImageInfo[] = [imageInfo];
await imageinfoFunction.default(context, getReq);
if (context.res != undefined)
{
expect(context.res.body).toEqual(JSON.stringify(expectedResult));
}
else
{
fail("context.res should not be undefined");
}
});
});
listBlobsByHierarchySpy.mockResolvedValue(() => listBlobsByHierarchyResponse);
出现以下错误: 类型'()=> ContainerListBlobHierarchySegmentResponse'的参数不能分配给'从不'类型的参数。
我尝试过:
// Gives the same error as above
listBlobsByHierarchySpy.mockResolvedValue(async() => listBlobsByHierarchyResponse);
// Says that it is missing a bunch of properties which make it hard to mock
listBlobsByHierarchySpy.mockImplementation(async() => listBlobsByHierarchyResponse);
任何帮助将不胜感激!